Thursday, December 24, 2009

Need help with Java coding return statements.?

I have NO idea how to do this.





public static boolean isPrime(int x)


{


int y = x;


int a = 0;


while(y %26gt;= 1)


{


if(x % y == 0)


{


a++;


}


y--;


}


if(a==2)


{


return true;


}


else


{


return false;


}





I don't understand what to do if I want to DO somethng with the return statement. For example, if it returns true, then I want:


System.out.println(x + '; is Prime!';);


otherwise, I need it the prime factorization (which I'm still working on...)


But I don't know what to put in the if-statement in the main:





public static void main(String args[])


{


for(int x=2; x%26lt;100; x++)


{


isPrime(x);


if(//right in here is where I need help!!!!)


{


System.out.println(x + '; is Prime!';);


}


else //if it's not prime, then it must be factored


{


primefactorization(x);


}


}


}Need help with Java coding return statements.?
public static void main(String args[])


{


for(int x=2; x%26lt;100; x++)


{


//isPrime(x);





if(isPrime(x)) // THIS IS THE KEY


{


System.out.println(x + '; is Prime!';);


}


else //if it's not prime, then it must be factored


{


primefactorization(x);


}


}


}








the isPrime function returns a boolean . the if condition also requires a Boolean. so u can put the isPrime func in the if condition. if x is prime,


isPrime(x) returns true and the statements in the if block are executed. if x is not prime the control is passed to your else block.Need help with Java coding return statements.?
( By first I mean vcshabu and last I mean mainman) Report Abuse

public static void main(String args[])


{


for(int x=2; x%26lt;100; x++)


{


if(isPrime(x))


{


System.out.println(x + '; is Prime!';);


}


else //if it's not prime, then it must be factored


{


primefactorization(x);


}


}


}
just put ur isprime function in ur if clause. the returnd bollean value wud drive the if statement.

No comments:

Post a Comment