简体   繁体   English

创建一个告诉数字是否为质数的程序时遇到问题

[英]Trouble with creating a program that tells whether or not a number is prime

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. 我刚开始使用Java,所以感觉好像看不到明显的东西。 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 == . 您使用的是赋值运算符= ,而不是比较运算符==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM