I cannot figure why this program that I came up won't work. I am supposed to make a program that takes uses inputted integer and finds out whether or not the number is prime. Right now the program just says every number is prime. I just started with java so I feel like I am just not seeing something obvious. Any help would be great, thanks guys.
public class assignment1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Insert Number:");
Scanner scan = new Scanner(System.in);
int variable1 = scan.nextInt();
boolean is_prime = true;
for(int x = 2; x < (variable1 - 1); x++){
if (variable1 % x == 0){
is_prime = false;
}
}
if (is_prime = true){
System.out.println("Prime");
}else if (is_prime = false){
System.out.println("Not Prime");
}
}
}
if (is_prime = true){
System.out.println("Prime");
}else if (is_prime = false){
System.out.println("Not Prime");
}
You're using the assignment operator =
, not the comparison operator ==
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.