OK. I tried a whole new different thing. Tell me if this is close. Two errors: "Syntax error on token "<", invalid AssignmentOperator" and "Syntax error on token "int", invalid ArgumentList. I tried to put the divisor = divisor + 1 down in the main, and to do that I needed to call the divisor variable from the prime method. I also tried to add that x must be positive.
Code:
package prime;
import java.util.Scanner;
public class Prime
{
static boolean prime(int x){
int divisor = 2; //initialize loop variables
boolean prime = true;
//loop condition
while ((x% divisor) == 0){
divisor < x;}
return false;
}
public static void main(String [] args) {
Scanner sc = new Scanner (System.in);
int x;
divisor = divisor(prime(int));
//user input
System.out.print("x = ");
x = sc.nextInt();
if (x < 0){
System.out.print("Number cannot be negative.");
}
divisor = divisor + 1;
if (prime) { System.out.print("You have found a prime number!");
}else{ System.out.print("I'm sorry. The number you have chosen is not prime.");
}
}
}
OK, so syntax issues and control structure issues...
You check if x is less than 0. If it is, you print a message. Then you continue running your program. You should either return inside the block, or have an else with the rest of your code in it.
The line:
Code:
divisor = divisor(prime(int));
Is not syntactically correct or needed. Take it out.
I don't think you know how functions work, so I will try to explain briefly. The only way that a function interacts with the calling code is through it's parameter list and what it returns. Variables in the function you call prime have no bearing on prime and vice versa. What happens in the function is completely independent of what's in your main function. A function can be called as many times as you need. The compiler already knows the type of your functions, so you don't need to mention them in your main function at all until you want to use them. You do so by referencing their name, then an open parenthesis, then the variables you are passing as arguments separated by commas, then a close parenthesis. If the function returns a value, you can put a variable and an assignment operator, =, before the call. In the case of returning a boolean, you could just use the function directly in an if as it looks like you were trying to do. For prime, this would look like:
Code:
if(prime(x)) {
//Print that x is prime
} else {
//Print that x is not prime
}
or
Code:
boolean isPrime = prime(x);
Remove
from main and move it into your prime function.
To call your prime function (we've been over this before) you need to pass it x. To do this, you would write:
You already have prime in a conditional, so the boolean returned from prime will be used how you expect.
That will get your main function right.
Your prime function is a bit off. You had the right loop control before with:
Inside prime you need to check the condition:
In an if. if that condition is true, you know something about x and you can return the proper value.
In your while loop you will also have to increment divisor so the behavior of the loop changes with each iteration. That means BEFORE the } ending the while loop you need a statement that increments divisor.
If the while loop finishes, and has not returned (so x % divisor never equalled 0) you know something about x, and you need to return appropriately.
-Lee