无法完成扫描仪输入

时间:2015-09-13 00:01:14

标签: java input console

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

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

编辑:我不知道如何在这里编号代码行,但我认为是第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();
    }
}
}

2 个答案:

答案 0 :(得分:0)

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

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。不要输入更多等级”。并停在那里。如果我们不包含break;语句,它将无限地打印出来。

答案 1 :(得分:0)

好的,根据我的经验,这似乎是一个相当困难的问题,可以根据你的代码来解决。在"继续输入成绩后,我找到了一个解决if语句的解决方案?"题。将enterAgain变量设置为等于userInput.next();这将允许程序继续其路径。虽然,从那里,我有一个问题,在你说'#34; y或n"该计划结束。这里:

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

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