【问题标题】:Simple test program looping issues简单的测试程序循环问题
【发布时间】: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


【解决方案1】:

我认为您的问题在于您的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 }

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2011-06-17
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多