程序打印提示输入,然后不输入第一个输入就打印下一个提示

时间:2019-04-17 03:49:35

标签: java input output jgrasp

我正在做一个分配工作,需要用户输入他们的汽车规格(年份,颜色,品牌,型号和最高速度)。输入作为汽车类别的参数给出,然后打印在句子中。输出示例:

“您有一台2003年的本田CR-V。最高时速为130英里/小时。”

它编译得很好,要求第一个输入(年份),但是跳过下一个输入。但这是输出:

当我在jgrasp调试器中逐行运行程序时,它允许所有输入,直到speedInput。

这是主程序的输入

      Scanner scnr = new Scanner(System.in);

      int yearInput;
      String colorInput;
      String makeInput;
      String modelInput;
      int speedInput;

      //INPUTS FOR CAR PARAMETERS
      System.out.println("Please enter your car's year:");
      yearInput = scnr.nextInt();  

      System.out.println("Please enter your car's color:");
      colorInput = scnr.nextLine();

      System.out.println("Please enter your car's make:");
      makeInput = scnr.nextLine();

      System.out.println("Please enter your car's model:");
      modelInput = scnr.nextLine();

      System.out.println("Please enter your car's top speed:");
      speedInput = scnr.nextInt();

这是当前输出。

Please enter your car's year:
2003                                     //user input
Please enter your car's color:
Please enter your car's make:
Honda                                     //user input
Please enter your car's model:
CR-V                                     //user input
Please enter your car's top speed:
130                                     //user input
You have a 2003  Honda CR-V. With a top speed of 130mph.

当我在调试器中逐行运行程序时...

Please enter your car's year:
2003
Please enter your car's color:
red
Please enter your car's make:
honda
Please enter your car's model:
CR-V
Please enter your car's top speed:
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at CarAssignment.main(CarAssignment.java:31)

0 个答案:

没有答案