首先声明,程序比较长,建议各位大哥大姐跑跑,当然大神如果用眼就能解决问题,我强烈支持。问题程序如下,已经完整化,可以直接跑:import java.util.Scanner;
import java.text.DecimalFormat;public class PizzaOrder
{
   public static void main (String [] args)
   {
      Scanner keyboard = new Scanner (System.in);      int o=0;                      //An object ust for cycle
      int i=0;                      //Also an object for cycle
      int inches = 12;              //size of the pizza
      String input;                 //user input
      double cost;                     //default price of the pizza      while(o==0){      System.out.println("Pizza Size (inches)   Cost");
      System.out.println("        10            $10.99");
      System.out.println("        12            $12.99");
      System.out.println("        14            $14.99");
      System.out.println("        16            $16.99");
      System.out.println("What size pizza would you like?");
      System.out.print("10, 12, 14, or 16 (enter the number only): ");
      inches = keyboard.nextInt();      switch(inches)
               {
                  case 10:cost=10.99;o=1;break;
                case 12:cost=12.99;o=1;break;
                case 14:cost=14.99;o=1;break;
                case 16:cost=16.99;o=1;break;
                default:System.out.println("Sorry,it's a wrong number.");
                System.out.println("So,a 12 inches' pizza will be made for you.");
                  i=0;
                while(i==0)
                  {
                    System.out.println("Is that OK? please type in 'Y'or'N' accurately.");
                    input=keyboard.nextLine();
                   if(input.equals("N")||input.equals("n")||input.equals("Y")||input.equals("y"))
                   {
                     if(input.equals("N")||input.equals("n"))
                              {
                                System.out.println("Please hold on, the system will be reset for you.");
                         o=0;
                         i=1;
                        }
                     else
                              {
                                o=1;i=1;
                        }
                          }
                  else
                    {
                      System.out.println("Sorry,the system can not identify,please enter again.");
                      i=0;
                     }
                      }
              }
              }
     }
}--------------------------------------------分割线--------------------------------------------------
这是我们课堂练习的一部分(接下来还有很长,我没有写),我稍作修改想完善一下系统,没想到出了一个让人费解的问题。
经楼主实测,此程序没有硬伤,能跑起来,但是第一次经过input=keyboard.nextLine()也就是红色部分的时候,不提示键盘输入,而是直接忽略。
首先把此程序的逻辑树说一下,这是披萨自动贩售系统关于披萨尺寸问询的部分:
一、问披萨的尺寸:“是要10,12,14还是16英寸的?”
  1.回答10、14、12或者16的时候,系统计入相应价格,并进入下一步(本部分结束)
  2.回答非上述四者的任何整数,则提示“输入错误,系统自动为您记为12寸的披萨,可以吗?”
    (1)回答Y,则进入下一步(结束)
    (2)回答N,则回到“一”部分
    (3)回答非Y、N的其他字符,则回复“无法识别”并跳回“2”部分。但是假如在“一”之下输入错误数字,并不会正确的显示到“2”部分,而是运行“2-(3)-2”而后才提示输入。
具体反馈如下:--------------------Configuration: <Default>--------------------
Pizza Size (inches)   Cost
        10            $10.99
        12            $12.99
        14            $14.99
        16            $16.99
What size pizza would you like?
10, 12, 14, or 16 (enter the number only): 1
Sorry,it's a wrong number.
So,a 12 inches' pizza will be made for you.
Is that OK? please type in 'Y'or'N' accurately.
Sorry,the system can not identify,please enter again.

Is that OK? please type in 'Y'or'N' accurately.红色部分就是第一次运input=keyboard.nextLine()语句被忽略的证据。PS:我想过把input=keyboard.nextLine()语句提前,并在最下面的else语句里面加入input=keyboard.nextLine()语句的方法,但是结果第一次的input=keyboard.nextLine()语句还是被略过。之后在while前后各加了一句input=keyboard.nextLine(),结果是第一次被略过,第二次可以正常提示输入。我估计这是不是JAVA的一个BUG,在switch的default语句中以及while的{}内,第一个input=keyboard.nextLine()语句将被忽略。而后我只能让它在while里面先循环一次,再显示出来,终于能行了……以下是更改的while循环部分:             while(i==0)
                 {
                     if(input.equals("N")||input.equals("n")||input.equals("Y")||input.equals("y"))
                      {
                         if(input.equals("N")||input.equals("n"))
                              {
                                System.out.println("Please hold on, the system will be reset for you.");
                               o=0;
                               i=1;
                              }
                         else
                              {
                                 o=1;i=1;
                             }
                         }
                    else
                       {
                             System.out.println("Is that OK? please type in 'Y'or'N' accurately.");
                            input = keyboard.nextLine();
                            if(input.equals("N")||input.equals("n")||input.equals("Y")||input.equals("y")){}
                            else
                                {
                                  System.out.println("Sorry,the system can not identify,please enter again.");
                                 i=0;
                               }
                      }
             }————————————————————还是分割线————————————————————
现在就想问一下,input=keyboard.nextLine()被略过的文帝到底怎么解决?

解决方案 »

  1.   

    是不是尼玛太长了,我问讲师,他说是nextLine和next的用法我不清楚,我很纠结……
      

  2.   

    纠正一个错误,最后我说道让它先在while里面循环一圈,其实还不到一圈,只是到了最下面的else而已,但是如果第一个input=keyboard.nextLine()是在else里面,是可以正常运行的。
      

  3.   

    我刚刚正式接触JAVA才几天而已啊,应该不会是什么很深奥的问题吧:(
      

  4.   

    什么意思?没看懂。你意思是说如果第一次输入错误数字,它显示
    Pizza Size (inches) Cost
      10 $10.99
      12 $12.99
      14 $14.99
      16 $16.99
    What size pizza would you like?
    10, 12, 14, or 16 (enter the number only): 1
    Sorry,it's a wrong number.
    So,a 12 inches' pizza will be made for you.
    Is that OK? please type in 'Y'or'N' accurately.
    而不是
    Pizza Size (inches) Cost
      10 $10.99
      12 $12.99
      14 $14.99
      16 $16.99
    What size pizza would you like?
    10, 12, 14, or 16 (enter the number only): 1
    Sorry,it's a wrong number.
    So,a 12 inches' pizza will be made for you.
    Is that OK? please type in 'Y'or'N' accurately.
    Sorry,the system can not identify,please enter again.
    Is that OK? please type in 'Y'or'N' accurately.
    吗?
      

  5.   

    在纠正一个错误:之所以更改之后的版本能够正常运行,是因为我在while之前加了一个input=keyboard.nextLine()!!然后我把input=keyboard.nextLine()打了两遍,不管在哪里,都能成功!!
      

  6.   

    修正六楼,改过之后的版本确实在while之前有个input=keyboard.nextLine(),但是删除之后依然可以正常运行!
      

  7.   

    第一个nextLine获取的是你前一行的换行符,之后一个才是你要的
      

  8.   

    仔細調試一下,怎麼會略過呢,可以單獨看它的表現你就清楚了:
    Scanner keyboard = new Scanner (System.in);
    String input=keyboard.nextLine();
    System.out.println(input);
      

  9.   

    哦sorry 沒看到之前的nextInt
    這我覺得就是因為nextInt不吸收換行符的緣故,因此之前的輸入直接被之後的nextLine吸收了
    初步建議改換輸入方式,比如都用nextLine
      

  10.   

    nextInt部分是没问题的。问题出在nextLine上,第一次用它会被忽略紧接着再写一遍,新写的就可以用了,但是之前的还是被忽略。求解
      

  11.   

    第二遍沒問題是因為第二遍之前不像第一遍有個nextInt
      

  12.   


    while (i == 0) {
    System.out.println("Is that OK? please type in 'Y'or'N' accurately.");
    input = keyboard.nextLine();
    if(input!=null){

    System.out.println(input.equals(""));
    }
    if (input.equals("N") || input.equals("n")代码太长,nextInt不吞换行符,所以nextLine()还没有输入就已经获取到了数据,一个换行符,上面的打印结果是true,所以,楼主如果想避免这样的问题,不妨在nextInt()后面加个nextline()就好了,省事。
      

  13.   

    补充:nextline()是捕捉到换行符才会向下执行。所以。。
      

  14.   

    Scanner keyboard = new Scanner (System.in);
    刚在while循环里
      

  15.   

    正式接触JAVA不过一星期啊,你们解释的明白点行不?????????