Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么下面的代码不能正常工作?(爪哇)_Java_Loops_If Statement_For Loop_Do While - Fatal编程技术网

Java 为什么下面的代码不能正常工作?(爪哇)

Java 为什么下面的代码不能正常工作?(爪哇),java,loops,if-statement,for-loop,do-while,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) { Sca

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

“我花了几个小时的时间在这件事上,它几乎奏效了。然而,当我输入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.");
        }
    }
}
import java.util.Scanner;
公共类Lab4a{
公共静态void main(字符串[]args){
Scanner scnr=new Scanner(System.in);//创建一个将接收用户输入的扫描仪
boolean isPrime=false;//最后,这是存储输入的“素数”的地方
int num;//将存储用户输入的变量
布尔值isOver=false;//一旦确定输入的“素数”,则为true
是否至少执行一次{//
System.out.println(“输入正整数或0退出:”;//提示用户输入
num=scnr.nextInt();//存储输入
如果(num==0){//如果该数字为零,则循环终止;如果为负,则循环也终止
系统出口(0);
}else if(num<0){
System.out.println(“请输入一个正整数”);
系统出口(1);
}

for(int mult=2;mult为
for
循环
for(int mult=2;multPut
isOver=true;
设置限制时出现问题,检查num是2还是3。因为
do while
循环永远不会结束。
或者,您可以在
for
循环开始之前结束
do while
循环。

布尔isPrime=false;
更改为
布尔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
import java.util.Scanner;
公共类Lab4a{
公共静态void main(字符串[]args){
Scanner scnr=new Scanner(System.in);//创建一个将接收用户输入的扫描仪
boolean isPrime=true;//最后,这是存储输入的“素数”的地方
int num;//将存储用户输入的变量
布尔值isOver=false;//一旦确定输入的“素数”,则为true
是否至少执行一次{//
System.out.println(“输入正整数或0退出:”;//提示用户输入
num=scnr.nextInt();//存储输入
如果(num==0){//如果该数字为零,则循环终止;如果为负,则循环也终止
系统出口(0);
}else if(num<0){
System.out.println(“请输入一个正整数”);
系统出口(1);
}

对于(int mult=2;mult)来说,这是您学习如何调试代码的绝佳机会。此外,您糟糕的时间管理也不是将您的问题推给一群互联网陌生人的理由。请将
isOver=true;
放在
if
块中。/me拉出计算器并检查2%的值2@rmlan “这对你来说是一个学习如何调试代码的绝佳机会。”我同意。这就是为什么我坚持了四个小时。但我宁愿跳过一次机会也不愿得到F。“此外,你糟糕的时间管理并不是把你的问题推给一群网络陌生人的理由。"这是你的个人观点。请陈述并停止将其混淆为事实。感谢你澄清此陈述是我的个人观点。我理解我所说的一切都是事实。可以说,这是你错过了学习如何操作的机会。希望你至少学到了一些东西。任何数字%1==0,所以所有数字都是w你会是最棒的。