Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 - Fatal编程技术网

Java 是什么导致了我的无限循环?

Java 是什么导致了我的无限循环?,java,loops,Java,Loops,我似乎无法找出我在创建无限循环的代码中做错了什么。如果有人能帮我解释一下,我将不胜感激 import java.util.Scanner; class LoopMath1 { public static void main(String[] args) { Scanner inputScanner; inputScanner = new Scanner(System.in); //gets a number from a user an

我似乎无法找出我在创建无限循环的代码中做错了什么。如果有人能帮我解释一下,我将不胜感激

import java.util.Scanner;

class LoopMath1 {
    public static void main(String[] args) {
        Scanner inputScanner;
        inputScanner = new Scanner(System.in);

        //gets a number from a user and parses the string as an int
        System.out.println("Please give me a positive number");
        String userNum;
        userNum = inputScanner.nextLine();
        System.out.println("Your number is " + userNum + ".");
        int number = Integer.parseInt(userNum);

        printX(number);  //function call

        //prints 2 to the x power
        System.out.print("2^" + number + "=");
        int j = 1;
        int twoToThe = 2;
        while (j < number) {
            twoToThe *= 2;
            j++;
        }
        System.out.print(twoToThe);

        //determines if the user number is prime
        int i = 0;
        for (i = 1; 1 < number; i++) {
            int nPrime = number;
            if (nPrime == 0) {
                System.out.println(number + " is not prime.");
                break;
            } else {
                System.out.println(number + " is prime.");
            }

        }
    }

    //this is a function to print a certain amount of Xs, depending          on the user input
    public static void printX(int nTimes) {
        final int WIDTH = nTimes;
        while (nTimes < WIDTH) {
            System.out.print("x");
            nTimes += 1;

        }
    }
} 
import java.util.Scanner;
类循环数学1{
公共静态void main(字符串[]args){
扫描器输入扫描器;
inputScanner=新扫描仪(System.in);
//从用户获取一个数字,并将该字符串解析为int
System.out.println(“请给我一个正数”);
字符串userNum;
userNum=inputScanner.nextLine();
System.out.println(“您的号码是“+userNum+”);
int number=Integer.parseInt(userNum);
printX(number);//函数调用
//将2打印到x幂
系统输出打印(“2^”+数字+“=”);
int j=1;
int twothe=2;
while(j
for(i=1;1
有打字错误<代码>1应为
i
,如(i=1;i替换

    for (i=1; 1 < number; i++) {
(i=1;1{

(i=1;i{
在for循环的第一部分中初始化的incrementor必须在第二部分中进行测试,最好在第三部分中进行增量测试。

用java标记这一点也很有用,可以查找“停止问题”.我认为Java在标准APIfinite循环中有一个解决方案…你检查了用户输入的数字吗?很抱歉,你收到了这么多的反对票,而没有人解释他们的反对票。(这是非常糟糕的形式,大家!)我假设他们是反对票,因为你的问题可以更简洁--“为什么这会产生一个无限循环?”(后面是简化的代码)。实际上,你的问题是“请帮我完成作业!”参见@DawsonToth nTimes for (i=1; i < number; i++) {