【问题标题】:Stuck inside while loop卡在while循环中
【发布时间】:2016-12-30 12:30:53
【问题描述】:

我正在尝试将 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);
    }
}

谢谢。

【问题讨论】:

  • 你调试过这个吗?因为你有两个循环,我猜这里有一个无限循环。但这也可能卡在扫描仪上。或者两者都^^
  • 调用 nextInt() 函数而不是将值存储在之前的变量中不是一个好主意....在每个 nextInt() 语句中,程序都希望您输入一个整数。
  • 是的,我调试了它,这就是为什么有很多打印语句@AxelH
  • 我不明白你在存储它之前存储它是什么意思......我有点初学者所以谢谢你们两个@progy_rock

标签: java


【解决方案1】:

它不会进入无限循环,而是您已经在 Scanner 中存储了两个值,您正在使用 hasNextInt() 检查它们。因此它始终为真并等待下一个输入进行检查。如果您输入 Int 值,它将处于相同的 while 循环中。输入像 String 这样的非整数退出 while 循环,您的程序将结束。 实际上,您正在等待两个 while 循环中的输入,因此它正在等待您的输入。

【讨论】:

  • 我是这里的初学者,所以我不确定我是否完全理解你.. hasNextInt() 总是正确的,当我达到这样的输入中的最后一个 1 时,它不应该是正确的“ 3 -1 -1 1"?..谢谢你的回答
【解决方案2】:

问题

您在系统输入流System.in 上使用Scanner。这意味着sc.hasNextInt() 尝试从底层流中获取下一个值,即System.in。但是,此流只会提示您输入新输入并将其返回给Scanner。一旦Scanner 收到换行符,它会检查之前的序列是否为int。如果您只按回车,则序列为,因此被忽略。如果您反复按回车,则不是无限​​期执行的循环,您的代码卡在sc.hasNextInt(),它没有获得新令牌(因为空序列),并再次询问底层流然后再次。

但是,如果您输入除int 以外的任何内容,例如0.2abc...,则Scanner 将返回false,因为该序列非空不是int

解决方案

如果您想保持您的代码不变,并且您希望hasNextInt() 在您按回车时返回false(仅换行符),您可以将您的Scanner 包装在这个包装器中:

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。由于您无法使用Scanner 推回您读取的最后一个令牌,因此此类临时存储它,因此对hasNextInt() 的多次调用不会跳过值。在您的方法中添加:

public static void main(String[] args) {

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

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

}

【讨论】:

  • 我开始明白了..我是否应该将第一个 while 循环更改为 while(d
  • 这是代码强制codeforces.com/contest/427/problem/A 的问题,对于每个正整数,我应该计算其后有多少个-1,并将其与正整数进行比较
  • 代码中的 z 为测试用例提供了正确的值..我只想让它脱离循环:D
  • 明确地说,如果您按回车键或提供int以外的任何其他类型(后者已经完成),您希望扫描仪返回false ?
  • 我添加了一个解决方案。但是,您的项目可以通过阅读两行来轻松解决,第一行用于n,第二行在链接中。然后你可以拆分后面的字符串并循环它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多