【发布时间】:2016-07-24 22:39:38
【问题描述】:
我在 Jimmy John's 工作,正在对三明治进行测试。我把基本结构记下来了,但是当你回答错误的问题时,它会继续下一个问题。我需要它重新提出问题,然后继续循环。代码如下:
package Practice;
import java.util.Scanner;
public class jimmyTest {
public static void main (String args[]){
int age, menuItem;
String tomato, lettuce, mayo, ham, turkey, tuna, beef, capicola, salami, cheese, onion, sauce, oregano, avacado, dijon, sprouts;
tomato = ("Tomato");
lettuce = ("lettuce");
mayo = ("mayo");
ham = ("ham");
turkey = ("turkey");
beef = ("beef");
capicola = ("capicola");
salami = ("Salami");
cheese = ("cheese");
onion = ("onion");
sauce = ("sauce");
oregano = ("oregano");
avacado = ("avacado");
dijon = ("dijon");
sprouts = ("sprouts");
tuna = ("tuna");
Scanner item = new Scanner(System.in);
Scanner item1 = new Scanner(System.in);
Scanner item2 = new Scanner(System.in);
Scanner item3= new Scanner(System.in);
Scanner item4 = new Scanner(System.in);
Scanner item5 = new Scanner(System.in);
Scanner item6 = new Scanner(System.in);
Scanner item7 = new Scanner(System.in);
Scanner item9 = new Scanner(System.in);
Scanner item10 = new Scanner(System.in);
Scanner item11 = new Scanner(System.in);
Scanner item12 = new Scanner(System.in);
Scanner item13 = new Scanner(System.in);
Scanner item14 = new Scanner(System.in);
Scanner item15 = new Scanner(System.in);
Scanner item16 = new Scanner(System.in);
Scanner item17 = new Scanner(System.in);
Scanner item18 = new Scanner(System.in);
Scanner finished = new Scanner(System.in);
String scan1, scan2, scan3, scan4, scan5, scan6, scan7, scan8, scan9, scan10, scan11, scan12, scan13, scan14, scan15, scan16, scan17, scan18, fin;
/*
* scan1 = item1.next();
* scan2 = item2.next();
* scan3 = item3.next();
* scan4 = item4.next();
* scan5 = item5.next();
* scan6 = item6.next();
* scan7 = item7.next();
* scan8 = item8.next();
* scan9 = item9.next();
* scan10 = item10.next();
* scan11 = item11.next();
* scan12 = item12.next();
* scan13 = item13.next();
* scan14 = item14.next();
* scan15 = item15.next();
* scan16 = item16.next();
* scan17 = item17.next();
* scan18 = item18.next();
* fin = finished.next();
*
*/
System.out.println("This is a test over the Jimmy Johns menu. a menu Item will appear, enter each");
System.out.println("item that goes into the sandwich and press enter as you type each item.");
menuItem = 0;
int points;
points = 0;
do {
switch (menuItem){
case 0:
System.out.println("What items are on the Slim 1 from the bread up");
scan1 = item1.next();
scan2 = item2.next();
if (scan1.contains(ham) && scan2.contains(cheese)){
System.out.println("Great job!");
menuItem =+ 1;
points++;
System.out.println(points);
}
else{
System.out.println("Lets try that again!");
}
//*****************************
case 1:
System.out.println("What items are on the Slim 2 from the bread up");
scan1 = item1.next();
if (scan1.contains(beef)){
System.out.println("Great job!");
menuItem =+ 1;
points++;
System.out.println(points);
}
else{
System.out.println("Lets try that again!");
menuItem = 1;
points = 1;
scan1 = item1.next();
}
//***********************************8
case 2:
System.out.println("What items are on the Slim 3 from the bread up");
scan1 = item1.next();
if (scan1.contains(tuna)){
System.out.println("Great job!");
menuItem =+ 1;
points++;
System.out.println(points);
}
else{
System.out.println("Lets try that again!");
menuItem = 2;
}
case 3:
System.out.println("What items are on the Slim 4 from the bread up");
scan1 = item1.next();
if (scan1.contains(turkey)){
System.out.println("Great job!");
menuItem =+ 1;
points++;
System.out.println(points);
}
else{
System.out.println("Lets try that again!");
menuItem = 3;
}
case 4:
System.out.println("What items are on the Slim 5 from the bread up");
scan1 = item1.next();
scan2 = item2.next();
scan3 = item3.next();
if (scan1.contains(salami) && scan2.contains(capicola) && scan3.contains(cheese)){
System.out.println("Great job!");
menuItem =+ 1;
points++;
System.out.println(points);
}
else{
System.out.println("Lets try that again!");
menuItem = 4;
}
case 5:
System.out.println("What items are on the Slim 6 from the bread up");
scan1 = item1.next();
scan2 = item2.next();
scan3 = item3.next();
if (scan1.contains(cheese) && scan2.contains(cheese)){
System.out.println("Great job!");
menuItem =+ 1;
points++;
System.out.println(points);
}
else{
System.out.println("Lets try that again!");
menuItem = 5;
}
break;
case 6:
System.out.println("What items are on the #1 Pepe from the bread up");
break;
case 7:
System.out.println("What items are on the #2 Big John from the bread up");
break;
case 8:
System.out.println("What items are on the #3 Totally Tuna from the bread up");
break;
case 9:
System.out.println("What items are on the #4 Turkey Tom from the bread up");
break;
case 10:
System.out.println("What items are on the #5 Vito from the bread up");
break;
case 11:
System.out.println("What items are on the #6 Veggie from the bread up");
break;
case 12:
System.out.println("What items are on the JJBLT from the bread up");
break;
case 13:
System.out.println("What items are on the #7 Smoked Ham Club from the bread up");
break;
case 14:
System.out.println("What items are on the #8 Billy Club from the bread up");
break;
case 15:
System.out.println("What items are on the #9 Italian Club from the bread up");
break;
case 16:
System.out.println("What items are on the #10 Hunters Club from the bread up");
break;
case 17:
System.out.println("What items are on the #11 Country Club from the bread up");
break;
case 18:
System.out.println("What items are on the #12 Beach Club from the bread up");
break;
case 19:
System.out.println("What items are on the #13 Veggie Club from the bread up");
break;
case 20:
System.out.println("What items are on the #14 Bootlegger Club from the bread up");
break;
case 21:
System.out.println("What items are on the #15 Club Tuna from the bread up");
break;
case 22:
System.out.println("What items are on the #16 Club Lulu from the bread up");
break;
case 23:
System.out.println("What items are on the #17 Ultimate Porker from the bread up");
break;
case 24:
System.out.println("What items are on the Gargantuan from the bread up");
break;
}
return;
}while(points < 24);
}
任何帮助将不胜感激!
谢谢,
昆腾
【问题讨论】:
-
在每个 case 子句的末尾添加
break语句。阅读 Java 中的switch。此外,您正在做无数错误的事情。我建议您找到一个在线教程来帮助您入门。创建几十个Scanner实例不是读取数据的方式 -
您只需要一个
Scanner即可指向System.in。如果您不打算更改它们或使用较短的名称,也不需要每个字符串值的变量。只需在代码中使用字符串文字即可。 -
您还使用
=+而不是+=。有很大的不同。一个分配一个正数,另一个将变量增加一个数字。 -
只使用一台扫描仪!!
-
放置一个while循环并在每个Q%A周围使用一个布尔值,如果他们做对了,循环控制变量将变为假,因此继续下一个问题
标签: java loops switch-statement