Java 卡在里面,而环

Java 卡在里面,而环,java,Java,我试图将z的值打印为输出,但我的代码没有完成执行。它到达了“here”行,但从未到达最后一行“z is” 我猜s=sc.nextInt()是问题所在 public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int x = 0; int y = 0; int z = 0; int u = 0; int n

我试图将z的值打印为输出,但我的代码没有完成执行。它到达了“here”行,但从未到达最后一行“z is”

我猜s=sc.nextInt()是问题所在

public class Solution {

public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);
    int x = 0;
    int y = 0;
    int z = 0;
    int u = 0;
    int n = sc.nextInt();
    int s = sc.nextInt();
    while(sc.hasNextInt()) {
        if(s != -1){
            y = s;
            if(sc.hasNextInt()){
                s = sc.nextInt();                 
            }
        }     
        while(s == -1){
            x++;
            System.out.println("s is "+s);
            z = Math.abs(y - x) + u;
            System.out.println("s is "+s);
            System.out.println("x is " + x+ " y is "+ y+" z is "+z);
            if(sc.hasNextInt()){
                s = sc.nextInt();                 
                System.out.println("s33 is "+s);
            }
        }
        if(z != 0){
            u = z;
        }    
        x = 0;
        y = 0;
        System.out.println("here");
    }
    System.out.println("z is" +z);
    }
}

谢谢。

它不是无限循环,而是您已经在扫描仪中存储了两个值,您正在使用hasNextInt()进行检查。因此它始终为true,并等待下一个输入进行检查。如果您继续输入Int值,它将处于相同的while循环中。输入类似非整数的字符串以退出while循环,程序将结束。 实际上,您正在两个while循环中等待输入,因此它正在等待您的输入。

问题 您正在系统输入流
系统中使用
扫描仪
。这意味着
sc.hasNextInt()
尝试从底层流中获取下一个值,即
System.in
。但是,此流只会提示您输入新内容,并将其返回到
扫描仪
。一旦
扫描器
接收到换行符,它将检查前面的序列是否为
int
。如果只按enter键,则序列为空,因此被忽略。如果您反复按enter键,则不会无限期地执行循环,您的代码会被卡在
sc.hasNextInt()
,它不会获得新标记(因为序列为空),并一次又一次地询问底层流

但是,如果输入的不是
int
,如
0.2
abc…
扫描仪将返回
false
,因为序列是非空的不是
int

解决方案 如果您希望保持代码的原样,并且希望在按下enter键(仅换行符)时
hasnetint()
返回
false
,则可以将
扫描仪
包装在此包装器中:

import java.util.Scanner;

public class ScannerWrapper {

    private Scanner scanner;
    private Integer current;

    public ScannerWrapper(Scanner scanner) {
        this.scanner = scanner;
    }

    public boolean hasNextInt() {

        // Current is not null, if method is called multiple
        // times, the value was checked already, it is an integer
        if (current != null) {
            return true;
        } else {

            // Reads line including newline character
            String nextLine = scanner.nextLine();

            try {
                // Try to covert the input to an integer
                current = Integer.parseInt(nextLine);
                return true;
            } catch (NumberFormatException e) {
                // Input is not an integer
                return false;
            }

        }
    }

    public int nextInt() {

        // Used the already checked value or request new input
        if (current != null) {
            int next = current;
            current = null;
            return next;
        } else {

            int next = scanner.nextInt();

            // Consume the newline character
            scanner.nextLine();

            return next;

        }
    }

}
如果可能的话,这个类读取完整的行并将它们转换成
int
。由于您无法回退使用
扫描仪读取的最后一个令牌
,因此此类会临时存储它,因此对
hasnetint()
的多次调用不会跳过值。在您的方法中,只需添加:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    ScannerWrapper sc = new ScannerWrapper(scanner);

    int x = 0;
    // Rest of your code...

}

你调试过这个吗?既然你有两个循环,我猜这里有一个无限循环。但这也可能卡在扫描仪上。或者两者都是^^调用then
nextInt()
函数而不是将值存储在变量中不是一个好主意……在每个
nextInt()
语句中,该程序希望您输入一个整数。可能是重复的是我调试了它这就是为什么有很多打印语句@AxelHi不明白在我将它存储到s之前存储它是什么意思。我是一个初学者,所以感谢您两个@progy_rock我是一个初学者,所以我不确定我是否完全理解您。hasNextInt()怎么样始终为真当我到达输入中的最后一个1(如“3-1-11”)时,它不应该为真…感谢你的回答,我开始得到它了..我是否应该将第一个while循环更改为while(dint
以外的任何其他类型(后者已完成),则希望扫描仪返回
false
?我添加了一个解决方案。但是,通过阅读两行代码,您的项目可以更容易地解决,第一行用于
n
,第二行如链接中所示。然后可以将后一个字符串拆分并在其上循环。