为什么以下代码无法正常工作? (JAVA)

时间:2017-03-22 16:55:04

标签: java loops if-statement for-loop do-while

我的目标是接收用户输入并找出它是否为素数。

'几个小时以来一直在努力,这几乎是有效的。但是,当我输入2或3时,我什么也得不到。 do-while循环只是跳到下一次迭代。

我创建的for循环不适用于2或3,所以我为它创建了一个单独的if语句。问题是,它不起作用。我不知道为什么除了它可能不会执行。

import java.util.Scanner;

public class Lab4a {
    public static void main (String [] args) {
    Scanner scnr = new Scanner(System.in); //creates a Scanner that will receive user input
    boolean isPrime = false; //this, in the end, is where the "primeness" of the input is stored
    int num; //the variable which will store the user input
    boolean isOver = false; //is true once the "primeness" of the input has been decided

    do { //executes at least once
        System.out.println("Enter a positive integer or 0 to exit:"); //prompts the user for input
        num = scnr.nextInt(); //stores the input

        if (num == 0) { //if the number is zero, the loop terminates; if it's negative, the loop terminates as well
            System.exit(0);
        } else if (num < 0) {
            System.out.println("Please enter a positive integer.");
            System.exit(1);
        }

        for (int mult = 2; mult <= num/2; mult++) { //divides the user input by 2, tests for if anything remains; increments by one up to the half of the number. If a remain is encountered,
                                                    // isPrime becomes false and isOver true and the for loop is terminated. If not, the for loop will end with isPrime true and isOver false
            if (num % mult == 0) {
                isPrime = false;
                isOver = true;
                break;
            } else {
                isPrime = true;
                isOver = true;
            }

        }

        if (num == 2 || num == 3) { //the for test above does not work if the user input is 2 or 3, so a separate if statement tests for that
            isPrime = true;
        }
    } while (!isOver); //if isOver is true, the while loop ends



        if (isPrime == true) { //prints the appropriate answer
            System.out.println(num + " is prime.");
        } else {
            System.out.println(num + " isn't prime.");
        }
    }
}

我通常会尝试自己解决这些问题,但是我再次一直在努力解决这个问题4个小时,我还有另外一个类似的程序要写,以及为明天做准备的微积分测验,我知道正好杰克狗屎。我真的很绝望。

TL; DR:当我输入2或3时,程序无法正常工作;可能是因为if语句没有运行。除此之外,我什么都不知道。

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

isOver = true;放在检查num是2还是3的位置。因为do while循环永远不会结束。 或者,您可以在do while循环开始之前结束for循环。

答案 1 :(得分:0)

boolean isPrime = false;更改为boolean isPrime = true;并删除整个else

import java.util.Scanner;

public class Lab4a {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in); //creates a Scanner that will receive user input
boolean isPrime = true; //this, in the end, is where the "primeness" of the input is stored
int num; //the variable which will store the user input
boolean isOver = false; //is true once the "primeness" of the input has been decided

do { //executes at least once
    System.out.println("Enter a positive integer or 0 to exit:"); //prompts the user for input
    num = scnr.nextInt(); //stores the input

    if (num == 0) { //if the number is zero, the loop terminates; if it's negative, the loop terminates as well
        System.exit(0);
    } else if (num < 0) {
        System.out.println("Please enter a positive integer.");
        System.exit(1);
    }

    for (int mult = 2; mult <= num/2; mult++) { //divides the user input by 2, tests for if anything remains; increments by one up to the half of the number. If a remain is encountered,
                                                // isPrime becomes false and isOver true and the for loop is terminated. If not, the for loop will end with isPrime true and isOver false
        if (num % mult == 0) {
            isPrime = false;
            isOver = true;
            break;
        }
    }
} while (!isOver); //if isOver is true, the while loop ends

答案 2 :(得分:-1)

设置for循环for (int mult = 2; mult <= num/2; mult++)的限制时出现问题。

当输入为2 num/2或3时,num/2返回1,条件运行即。 mult<=2失败。