【问题标题】:Unable to complete Input for Scanner [duplicate]无法完成扫描仪的输入 [重复]
【发布时间】:2015-09-13 00:01:14
【问题描述】:

我正在制定一个平均成绩的计划。出于某种原因,我的扫描仪或控制台不允许我输入第三个问题的答案,我不知道为什么。

很多代码被注释掉了,因为它现在不需要。

编辑:我不知道如何在这里给代码行编号,但我认为第 23-28 行是问题

import java.text.DecimalFormat;
import java.util.Scanner;

public class GradeData {

    public static void main(String[] args) {

int count = 0;
double grade = 0;
//decided to use double for grade in case user's want to be specific with
//decimal points.
String user;
boolean enter;
String name;
String enterAgain;
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter your name: ");
name = userInput.nextLine();
System.out.println("Please enter a grade: ");
grade = userInput.nextDouble();

//System.out.println(user); //May not need this line
System.out.println("Would you like to continue entering grades? (y/n)");
enterAgain = userInput.nextLine();

if (enterAgain.equalsIgnoreCase("y"))
        /*|| enterAgain.equalsIgnoreCase("Yes"))*/ {

    System.out.println("Please enter another grade: "); }
/*  if (user.getScore() > highScore) {

        highScore = user.getScore();
    }
    System.out.println("High Score: "
            + DecimalFormat.format(grade));
    user.setScore(0);
}*/

else {
    enter = false;
userInput.close();
    }
}
}

【问题讨论】:

    标签: java input console


    【解决方案1】:

    查看@Fast Snail 评论的链接 (Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods) - 它解释了您的问题。要解决此问题,如该帖子所示,请在 userInput.nextDouble() 之后添加 userInput.nextLine()。这是完成的代码(我删除了一些不相关的 cmets 和变量):

    import java.util.Scanner; 
    
    public class GradeData{
        public static void main(String[] args){
            double grade = 0;
            String name;
            String enterAgain;
            boolean enter = true; //to use in while loop
            Scanner userInput = new Scanner(System.in);
            System.out.println("Please enter your name: ");
            name = userInput.nextLine();
            System.out.println("Please enter a grade: ");
            grade = userInput.nextDouble(); 
            userInput.nextLine() //add this to your program
            System.out.println("Would you like to continue entering grades? (y/n)");
        enterAgain = userInput.nextLine(); 
    
            while(enter){
                if(enterAgain.equalsIgnoreCase("y")){
                    System.out.println("Please enter another grade: ");
                    /*enterAgain = userInput.nextLine() - we want user to input 
                    a double grade, not a String*/
                    grade = userInput.nextDouble();
                }
                else if(enterAgain.equalsIgnoreCase("n")){
                    System.out.println("Ok. Don't enter more grades.");
                    break; 
                }
                else{
                    System.out.println("Sorry, you can only enter (y/n)");
                    break; 
                }
            }
        }
    }
    

    else{} 语句中的break; 语句用于在用户输入 n 表示 no(或除 y 或 n 之外的其他随机字符)后停止循环运行。例如,循环将输出“Ok. Don't enter more grades”。并停在那里。如果我们不包含break; 语句,它将无限打印出来。

    【讨论】:

    • 感谢您的帮助,这些编辑效果很好。您是否碰巧知道一个可以使用该 if 语句的良好循环?
    • @Martin 是的。看看我更新的代码。它允许用户在输入 y 后继续输入等级。我试图做到这一点,以便他们可以输入 n 并在他们已经输入 y 后退出循环 - 但这没有成功。仍在试图弄清楚这一点。所以几乎在你输入 y 之后,你一直被要求输入成绩而无法停止。
    【解决方案2】:

    好的,根据我的经验,根据您的代码,这似乎是一个相当困难的问题。在“继续输入成绩?”之后,我找到了一种进入 if 语句的解决方案?问题。设置 enterAgain 变量等于 userInput.next();这将允许程序继续其路径。虽然,从那里开始,我遇到了一个问题,在你说“y 或 n”之后程序结束。这里:

    System.out.println("Would you like to continue entering grades? (y/n)");
    enterAgain = userInput.next();
    

    此代码可让您输入内容,但不会显示“请输入其他成绩”。这是我能想出的唯一解决方案。还可以尝试使用 switch 语句或 while 循环来试验程序中的结果。祝你好运。

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 2018-05-30
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多