0

这部分代码应该加载餐厅名称,然后是菜单名称,然后是餐品名称和价格,但是当我运行它时,它会输入所有名称,然后当最终涉及价格时,我输入例如 7.2 并得到输入不匹配异常

  String newMenu = "";
  String newRestaurant = "";
  String[] newMenuItem = new String[10];
  double[] price = new double[10];
  int x = 0;  


       while (!(newMenu.equals("none"))) {
           System.out.println("What is the name of the Menu you wish to create (type 'none', if you are done):");
           newMenu = scan.next();
           if (newMenu.equals("none")) {
              System.out.println("Saving entry...");
              continue;

           } else {


              System.out.println("What is the name of the Menu item you wish to create (type 'none', if you are done):");
              newMenuItem[x] = "end";
              while (!(newMenuItem[x].equals("none"))) {
                 newMenuItem[x] = scan.next();

                 if (!(newMenuItem[x].equals("none"))) {
                    System.out.println("What is the price?");
                    price[x]= scan.nextDouble();
                    x++;


                    }    
                 }    
              }
           }
4

1 回答 1

0

如果我理解您程序的输入,最简单的解决方案是在第 79 行和第 80 行之间添加类似这样的内容,

while (!scan.hasNextDouble()) {
  // The next token isn't a double.
  if (scan.hasNext()) {
    scan.next();
  } else { 
    System.err.println("Nothing to read");
    System.exit(1);
  }
}

因为,目前您Scanner没有下一个符号作为double.

于 2014-06-09T01:24:57.800 回答