当我再次运行程序时跳过一行

时间:2013-11-19 02:24:59

标签: java

我有一个程序,它将接受用户输入的品牌,型号,车牌和汽车年份,并返回汽车租赁店等关键代码。当我问用户是否想要租用另一辆车时,他说是的,程序将再次运行。但是,当它再次运行时,它完全不允许用户输入品牌。这很奇怪,因为程序中的其他所有内容都有效。以下是我的代码的主要部分。

 public void run()

{

 input();    

addStuff();

printCode();

  String answer;
  System.out.print("Would you like to rent another car?(Yes or no) ==> ");
  answer =keyboard.next();
  switch(answer)
  {
      case "Yes": case "y": case "YES": case "yes": run();
      break;
      case "No": case "n": case "NO": case "no": System.out.println("Thanks for using Jake's Car Rental Services!");
      break;
      default: System.out.println("Are you sure you can drive? You cant even say yes or no!");
      break;
  }


 }//end run


 public void input()

{

System.out.println("Welcome to Jake's Car Rental Dealership!");

System.out.prinln();

   System.out.print("What is the make of your car? ==> ");
   myMake = keyboard.nextLine();
   System.out.println();
   System.out.print("What is the model and year of your car?(Space between model and year) ==> ");
   myModel = keyboard.next();

   System.out.println();
   System.out.print("What is your liscense plate ID?(Space between each number and letter) ==> ");
   myPlate = keyboard.next();
   myLetter1 = myPlate.charAt(0);
   myLetter2 = myPlate.charAt(1);
   myLetter3 = myPlate.charAt(2);
   myNum = keyboard.nextInt();
   System.out.println();
   print();
   getPlate();


 }//end input

2 个答案:

答案 0 :(得分:0)

您可以尝试使用nextLine而不只是next()

answer = keyboard.nextLine();

您使用的是java 7吗?

答案 1 :(得分:0)

问题可能是nextInt()不会占用新行。因此,随后在程序的第二次运行中或在程序的第二次运行中获取结果输入