【问题标题】:Is there a way to ensure a while loop does not run one time before asking for input?有没有办法确保 while 循环在要求输入之前不会运行一次?
【发布时间】:2020-01-29 03:42:33
【问题描述】:

我正在编写一个抛硬币程序。基本上,询问用户他们想玩多少游戏。然后,while 循环运行游戏,要求用户输入几个由空格分隔的掷硬币(正面或反面,例如 H H H T H)。然后程序将用户输入的字符串转换为一个数组,for循环遍历数组并检查heads(大写H)并将其存储在heads变量中。分数的计算方法是将数组中的正面数除以数组的长度,然后将其乘以 100 得到一个百分比。如果得分为 50% 或以上,则玩家获胜。如果低于 50%,玩家就输了。我发现的两个错误是,在我输入任何输入之前,while 循环已经运行了一次。此外,分数似乎计算不正确,它总是返回 0.0% 或 100.0%。我认为它与 inputArray.length 有关,但我不能说。谢谢。

import java.util.Scanner;
public class HeadTailGenerator {

public static void main (String []args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("How many games?");
    int games = scanner.nextInt();
    int count = 0;
    int heads =0;

    while (count<games){
        System.out.print("Enter your flips for game "+ count+": ");
        String input = scanner.nextLine();
        String [] inputArray = input.split("\\s+");
        for (int i = 0; i <inputArray.length-1; i++){

            if (inputArray[i].equals("H")){
                heads++;
            }

        }//exit for loop

        double score = (heads/(inputArray.length)*100);

        if (score >= 50.0){
            System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You win!");
        }
        else {
            System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You lose!");
        }

        count++;
    }
}
}

【问题讨论】:

标签: java arrays while-loop


【解决方案1】:

添加一些打印可以帮助您调试所有问题。

第一个问题是nextInt() 没有捕获用于输入游戏数的换行符,因此您可以在其后添加scanner.nextLine(); 以防止游戏立即结束。

第二个问题是你的 for 循环中的条件。移除 -1 以循环遍历数组中的所有元素。

第三个问题是,在计算得分时,您将整数除以始终大于或等于该整数的数字,因此小数被截断或结果为 1。您可以转换整数在运算中加倍或将heads 声明为双倍,或者如果您不关心百分比结果的小数,则可以在除法之前乘以 100。

import java.util.Scanner;
public class Main {
    public static void main (String []args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many games? ");
        int games = scanner.nextInt();
        System.out.println("Games: "+ games);
        scanner.nextLine();
        int count = 0;
        int heads = 0;

        while (count<games){
            System.out.print("Enter your flips for game "+ count+": ");
            String input = scanner.nextLine();
            String [] inputArray = input.split("\\s+");
            for (int i = 0; i <inputArray.length; i++){
                System.out.println("Input["+ i +"]: "+ inputArray[i]);
                if (inputArray[i].equals("H")){
                    heads++;
                    System.out.println("Heads count: "+ heads);
                }

            }//exit for loop

            double score = ((double)heads/(inputArray.length)*100);

            if (score >= 50.0)
                System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You win!");
            else 
                System.out.println("Game "+ count + ": "+ heads + " heads ("+ score+ "%); You lose!");

            count++;
        }
    }
}

【讨论】:

    【解决方案2】:

    不久前我遇到了这个问题。第一个问题,为什么 while 循环在接受用户输入之前运行一次是因为扫描器处理读取 nextInt() 的方式。 Scanner 刚刚读取了它到达的下一个整数值,但还没有到达行尾。因此,当您在 while 循环中调用 scanner.nextLine() 时,它实际上只是读取输入第一行的结尾,这就是您在那里得到零的原因。

    第二个问题,总是为零,与您使用的数据类型有关。请记住,int / int = int。因此,当您除以 3/4 = 0 时,因为 int 被截断。 存储在双精度中并不重要,是整数除法出错了。如果您只是将头部更改为浮动,它将纠正该问题。

    您可以了解更多关于扫描仪here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 2011-02-08
      • 2016-07-20
      相关资源
      最近更新 更多