首先声明,程序比较长,建议各位大哥大姐跑跑,当然大神如果用眼就能解决问题,我强烈支持。问题程序如下,已经完整化,可以直接跑: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()被略过的文帝到底怎么解决?
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()被略过的文帝到底怎么解决?
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.
吗?
Scanner keyboard = new Scanner (System.in);
String input=keyboard.nextLine();
System.out.println(input);
這我覺得就是因為nextInt不吸收換行符的緣故,因此之前的輸入直接被之後的nextLine吸收了
初步建議改換輸入方式,比如都用nextLine
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()就好了,省事。
刚在while循环里