简单的测试程序循环问题

时间:2016-07-24 22:39:39

标签: java loops switch-statement

我在Jimmy John工作,我正在为三明治做测试。我得到了基本的结构,但当你回答错误的问题时,它会继续下一个问题。我需要它重新问问题然后继续循环。这是代码:

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);
}

任何帮助都将非常感激!

谢谢,

昆滕

1 个答案:

答案 0 :(得分:0)

我认为你的问题在于你的menuItem =+ 1;陈述。 =+实际上不是受支持的java运算符。实际的代码应该是

menuItem += 1;

这种差异可能看似微不足道,但您的代码基本上是这样做的:

menuItem = +1;

+1可能等于2?我不确定。但我不认为它做了你认为它做的事情。无论如何,我倾向于删除所有这些陈述,因为当他们回答错误时你不需要改变menuItem。如果你保持不变,循环将继续使用相同的menuItem,这似乎是你想要的。只有在成功答案时增加。

另外需要注意的是String.Contains区分大小写,并且您似乎与菜单项声明不一致。你可以试试这个:

scan1.EqualsIgnoreCase(tuna);

此外,对于每个问题,您都不需要新的Scanner()。您可以将扫描程序重复用于多个输入,就像使用item1多次执行一样。

因此,您可以通过替换以下内容来清理代码:

scan1 = item1.next();
scan2 = item2.next();
scan3 = item3.next();

string salamiTest = testScanner.next();
string capicolaTest = testScanner.next();
string cheeseTest = testScanner.next();

if (salamiTest.EqualsIgnoreCase(salami) /* && otherTests */) {
    points++;
    menuItem++;
}
// else { they are wrong, don't increment menuItem }