我在switch语句中重复while循环时遇到问题,该循环继续重复

时间:2018-11-19 02:40:22

标签: while-loop

我是一名新手编码员,在上一节课时,我需要制作类似自动售货机的代码。但是,我将switch语句用作其中的一部分。我面临的一个问题是,在那些if语句中我添加了一张支票。检查的目的是询问用户是否要继续,但是由于某种原因,即使在输入后它也会重复。我不确定是否曾经问过这个问题,但是我的许多朋友都分享了这个问题。

import java.util.Scanner;
public class hw52 {
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    String choice;
    String answer = "";
    int cr = 0;
    int wb = 0;
    int ch = 0;
    int k = 0;
    int j = 0;
    boolean check =  true;
    double cost2;
    double discount;
    double change = 0;
    double cost = 0;
    double money = 0;
    int counter = 0;
    do {
        answer = "";
        System.out.println("Select an option in the vending machine. \n 
Crunch:$1.50 \n Water bottle:$2.00 \n Chips:$1.00 \n Ketchup:$4.00 \n 
Juice:$2.50");
        choice = keyboard.nextLine();
        switch(choice.toUpperCase()){
        case "CRUNCH":
            cost += 1.5;
            cr ++;
            counter ++;
            System.out.println("Cost: " + cost);
            while(check = true) {
            System.out.println("Do you want to continue? Yes or no?");
            answer = keyboard.nextLine();
            if (answer.equalsIgnoreCase("Yes")) {
                check = false;
            }else if(answer.equalsIgnoreCase("Yes")) {
                check = false;
            }else {
                System.out.println("Bad input.");
            }
            }
        case "WATER BOTTLE":
            cost += 2.00;
            wb++;
            counter ++;
            System.out.println("Cost: " + cost);
            while(check = true) {
                System.out.println("Do you want to continue? Yes or no?");
                answer = keyboard.nextLine();
                if (answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else if(answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else {
                    System.out.println("Bad input.");
                }
                }
        case "CHIPS":
            cost += 1.00;
            ch++;
            counter ++;
            System.out.println("Cost: " + cost);
            System.out.println("Cost: " + cost);
            while(check = true) {
                System.out.println("Do you want to continue? Yes or no?");
                answer = keyboard.nextLine();
                if (answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else if(answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else {
                    System.out.println("Bad input.");
                }
                }
        case "KETCHUP":
            cost += 5.00;
            k++;
            counter ++;
            System.out.println("Cost: " + cost);
            while(check = true) {
                System.out.println("Do you want to continue? Yes or no?");
                answer = keyboard.nextLine();
                if (answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else if(answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else {
                    System.out.println("Bad input.");
                }
                }
        case "JUICE":
            cost += 2.50;
            j++;
            counter ++;
            System.out.println("Cost: " + cost);
            while(check = true) {
                System.out.println("Do you want to continue? Yes or no?");
                answer = keyboard.nextLine();
                if (answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else if(answer.equalsIgnoreCase("Yes")) {
                    check = false;
                }else {
                    System.out.println("Bad input.");
                }
                }
        default: 
            System.out.println("Not a choice! Use exact names.");
            answer = "yes";
        }
    }while(!answer.equalsIgnoreCase("no"));
    counter /= 3;
    for (int i = 0; i < counter;i++) {
        discount = (double)(Math.random()*.1)+.1;
        cost2 = cost * discount;
        cost -= cost2;
    }   
    while(money < cost) {
    System.out.println("How much money would you like to use to pay?");
    money = keyboard.nextDouble();
    if (money < cost) {
        System.out.println("That's not enough money.");
    }
    }
    double Q = cost / .25;
    double D = (cost % .25) / .10;
    double N = ((cost % .25) % .10) / .05 ;
    double P = (((cost % .25) % .10) %.05) /.01;
    System.out.println("The items you bought are: \n Crunch Bars: " + cr + 
"\n Water Bottles: " + wb + "\n Chips: " + ch + "\n Ketchup: " + k + "\n 
Juice: " + j);
    System.out.println("Your change is: $" + ((Q * .25) + (D * .1) + (N * 
.05) + (P*.01))); 
    }

}

请原谅我凌乱的编码风格。我个人更喜欢它,但是我知道很多人觉得它很奇怪。

1 个答案:

答案 0 :(得分:0)

您的代码永远不允许用户在answer变量中输入“ no”。因此,您的代码始终会强迫用户输入“是”,或者显示消息“输入错误”。

每个if循环中都有这个while语句。 ifelse if都检查完全相同的条件。我假设您可能希望其中一个检查答案是否定的,除非选择“是”,否则check变量永远不会分配给false

answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("Yes")) {  //this is same condition as next if line.
    check = false;
}else if(answer.equalsIgnoreCase("Yes")) { //this is same condition as prior if line.
    check = false;
}else {
    System.out.println("Bad input.");
}

这也是您的主要问题。您总是在check循环条件中将true分配给while。您要使用==运算符。

这会将true赋给代码中的check变量:

while(check = true) { 

您实际上是想这样做:

while(check == true) {

我建议将IDE与调试器一起使用,该调试器允许您系统地逐步执行代码,并查看每行代码在执行时发生了什么。它将帮助您发现类似这样的逻辑问题。