我运行代码

时间:2017-04-04 03:02:09

标签: java

我正在使用:

public static final Scanner CONSOLE = new Scanner(System.in);

然后在我的主要方法中:

System.out.print("\nPlease choose a color for the circle between the following options:\nBlack\nRandom");
String colorChoice = CONSOLE.nextLine();
colorChoice = colorChoice.trim();

程序显示"请选择颜色"但在允许任何用户输入之前终止。我无法弄清楚它为什么不起作用。

这是输出:

请输入50到400之间圆圈的半径:[DrJava输入框]

请为以下选项之间的圆圈选择颜色: 黑色 随机>

编辑1:

我将行System.out.print("\nPlease choose a color for the circle between the following options:\nBlack\nRandom");更改为System.out.print("\nPlease choose a color for the circle between the following options: Black, Random");以查看\n是否是问题。它仍然无效。

我在(检查值)之后包含了一个while循环,因此代码为:

System.out.print("\nPlease choose a color for the circle between the following options: Black, Random");
String colorChoice = CONSOLE.nextLine();
colorChoice = colorChoice.trim();
boolean matchesChoice = matchesChoice(colorChoice, "black", "random");   
while(matchesChoice != true){
System.out.print("\nInvalid color choice. Please try again."); 
colorChoice = CONSOLE.nextLine();
colorChoice = colorChoice.trim();
matchesChoice = matchesChoice(colorChoice, "black", "random");
}

此输出显示:

请输入50到400之间圆圈的半径:[DrJava输入框]

请为以下选项之间的圆圈选择颜色:黑色,随机 颜色选择无效。请再试一次。 [DrJava输入框]

因此CONSOLE.nextLine()在循环内部工作,但不在外部。无论我作为用户输入插入什么(正确与否),它都会重复"无效颜色选择"线和输入。

2 个答案:

答案 0 :(得分:-1)

    import java.util.Scanner;
    import javax.swing;

    public class (NAME) {
        public static void main(String args[])throws Exception{
             Scanner CONSOLE = new Scanner(System.in);
             System.out.println(("Please enter a radius for the circle between 50 and 400")
             int radius = Integer.parseInt(JOptionPane.showInputDialog("Please choose a number from 50 to 400:"));
             System.out.println("Radius is " + radius.toString();
             System.out.println(("Please choose a color for the circle between the following options: Black Random");
             String colorChoice = CONSOLE.nextLine();
             colorChoice = colorChoice.trim();
->           System.out.println("You chose " + colorChoice);
    }

箭头所在的行缺少代码。它实际上接受colorChoice的值,但是因为忘记显示它而不显示它。 :)

    import java.util.Scanner;
    import javax.swing;

    public class (NAME) {
        public static final Scanner CONSOLE = new Scanner(System.in);
        public static void main(String args[])throws Exception{

             System.out.println(("Please enter a radius for the circle between 50 and 400")
             int radius = Integer.parseInt(JOptionPane.showInputDialog("Please choose a number from 50 to 400:"));
             System.out.println("Radius is " + radius.toString();
             System.out.println(("Please choose a color for the circle between the following options: Black Random");
             String colorChoice = CONSOLE.nextLine();
             colorChoice = colorChoice.trim();
->           System.out.println("You chose " + colorChoice);
    }

这也可以。如果您希望扫描仪是公开的。如果您需要,它可以是私有的,特别是当您只使用一个文件/类时。

PS:无缘无故地总是让选民失望。即使只是查看问题本身,也可能需要多个答案,我们大多数人都不知道具体提供什么。

答案 1 :(得分:-2)

尝试使用

while(CONSOLE.hasNextLine()) 

在将其分配给colorChoice之前。