在switch语句中添加double

时间:2016-10-19 21:20:18

标签: java

我正在创建一个包含一段时间的开关,用于作业。一切正常,除了添加到totalPrice双,我尝试使用

totalPrice += priceA;totalPrice = totalPrice + priceA;

这是我的转储,我知道这是一件非常简单的事情,但我的谷歌搜索和文档都没有了。

 
import java.util.Scanner;

public class Coffee {
    public static double priceA = 1.99;
    public static double priceE = 2.99;
    public static double priceL = 3.99;
    public static double totalPrice = 0;
    public static int custChoice;
    public static int counter =1;
    public static void main(String[] args) {
        menu();
         while (counter <3 && custChoice !=0) {
             menu();
             switch (custChoice) {
                 case '1':
                     totalPrice = totalPrice +priceA;
                     return;
                 case '2':
                     totalPrice = totalPrice +priceE;
                     return;
                 case '3':
                     totalPrice = totalPrice +priceL;
                     return;
                 case '0':
                     break;
                 default:
                     System.out.println("Your total is $" + totalPrice);
             }
             counter++;
         }
    }

    public static void menu() {
        Scanner input = new Scanner(System.in);
        System.out.println("Counter: " + counter);
        System.out.println("Total Price: $" + totalPrice);

        System.out.println("-----------------------------");
        System.out.println("  (1) American    1.99       ");
        System.out.println("  (2) Espresso    2.99       ");
        System.out.println("  (3) Latte       3.99       ");
        System.out.println("-----------------------------");
        System.out.println("");
        System.out.println(" Please make a selection \n\t or enter 0 to total and quit.");
        custChoice = input.nextInt();
    }
}

帮助我的StackOverflow!你是我唯一的希望。

7 个答案:

答案 0 :(得分:2)

试试这个,你将在第三选择后退出

public class Coffee {
    public static double priceA = 1.99d;
    public static double priceE = 2.99d;
    public static double priceL = 3.99d;
    public static double totalPrice = 0d;
    public static int custChoice;
    public static int counter =1;
    public static void main(String[] args) {
        menu();
        while (counter <3) {
            switch (custChoice) {
                case 1:
                    totalPrice = totalPrice +priceA;
                    break;
                case 2:
                    totalPrice = totalPrice +priceE;
                    break;
                case 3:
                    totalPrice = totalPrice +priceL;
                    break;
                case 0:
                    System.out.println("Your total is $" + totalPrice);
                    return;
                default:
                    break;
            }
            counter++;
            menu();
        }
    }

    public static void menu() {
        Scanner input = new Scanner(System.in);
        System.out.println("Counter: " + counter);
        System.out.println("Total Price: $" + totalPrice);

        System.out.println("-----------------------------");
        System.out.println("  (1) American    1.99       ");
        System.out.println("  (2) Espresso    2.99       ");
        System.out.println("  (3) Latte       3.99       ");
        System.out.println("-----------------------------");
        System.out.println("");
        System.out.println(" Please make a selection \n\t or enter 0 to total and quit.");
        custChoice = input.nextInt();
    }
}

答案 1 :(得分:1)

删除您正在检查的数字周围的单引号,否则switch语句正在查找字符。此外,似乎您可能希望使用break而不是return

         switch (custChoice) {
             case 1:
                 totalPrice = totalPrice +priceA;
                 break;
             case 2:
                 totalPrice = totalPrice +priceE;
                 break;
             case 3:
                 totalPrice = totalPrice +priceL;
                 break;
             case 0:
                 break;
             default:
                 System.out.println("Your total is $" + totalPrice);
         }

答案 2 :(得分:1)

CustChoice是字符串还是int?或者这是相关的吗?它看起来像在while条件下你将它作为一个int检查并在交换机中作为一个字符串,如果它是一个int,那么switch应该只是默认。无法测试它,但值得一看。

答案 3 :(得分:1)

这应该适合你:

public class Coffee {
        public static double priceA = 1.99;
        public static double priceE = 2.99;
        public static double priceL = 3.99;
        public static double totalPrice = 0;
        public static int custChoice;
        public static int counter = 1;

        public static void main(String[] args) {
             while (counter<=3) {
                 menu();
                 switch (custChoice) {
                     case 1:
                         totalPrice += priceA;
                         break;
                     case 2:
                         totalPrice += priceE;
                         break;
                     case 3:
                         totalPrice += priceL;
                         break;
                     case 0:
                         System.out.println("Your total is $" + totalPrice);
                         return;
                     default:
                         System.out.println("Unrecognised command");
                 }
                 counter++;
             }
        }

        public static void menu() {
            Scanner input = new Scanner(System.in);
            System.out.println("Counter: " + counter);
            System.out.println("Total Price: $" + totalPrice);

            System.out.println("-----------------------------");
            System.out.println("  (1) American    1.99       ");
            System.out.println("  (2) Espresso    2.99       ");
            System.out.println("  (3) Latte       3.99       ");
            System.out.println("-----------------------------");
            System.out.println("");
            System.out.println(" Please make a selection \n\t or enter 0 to total and quit.");
            custChoice = input.nextInt();
        }
    }

首先你要调用menu(),然后再调用menu()。您只需要调用一次,因为while循环将始终执行,因为计数器从1开始执行。

确保您将案例检查为整数而不是字符,并确保突破switch语句而不是像您一样返回。

希望这有帮助!

答案 4 :(得分:1)

你的问题是你检查交换机中的字符。用正确的数值替换那些。还可以使用break来摆脱循环而不是return一个简单的解决方法是给你的while循环一个标签,并在需要时使用它来打破。

theBank: while (counter <3 && custChoice !=0) {
                 menu();
                 switch (custChoice) {
                     case 1:
                         totalPrice += priceA;
                         break;
                     case 2:
                         totalPrice += priceE;
                         break;
                     case 3:
                         totalPrice += priceL;
                         break;
                     case 0:
                         break theBank;
                     default:
                         System.out.println("You did not enter correct output");
                 }
                 counter++;
             }
System.out.println("Your total is $" + totalPrice);

希望这会有所帮助。干杯:)

答案 5 :(得分:1)

你可以使用默认情况进行一些基本的输入错误处理,因为它所包含的代码将在没有检测到任何其他情况(0,1,2,3)时执行。

所以,正如其他人所提到的那样,将总价格的总和移到案例0,并用一个将终止程序执行的return语句结束该案例。

进一步在默认情况下为用户插入一些警告文本,并告诉输入错误。 为了防止出现逻辑问题,你应该将Counter减1,因为用户在while循环的迭代中没有购买任何东西,并且它将在循环结束时递增1

编辑: 我认为DánielKis的答案已经非常好了,但是对于默认情况没有任何内容。 我采取了他的解决方案并扩展了它:

public class Coffee {
public static double priceA = 1.99d;
public static double priceE = 2.99d;
public static double priceL = 3.99d;
public static double totalPrice = 0d;
public static int custChoice;
public static int counter =1;
public static void main(String[] args) {
    menu();
    while (counter <3) {
        switch (custChoice) {
            case 1:
                totalPrice = totalPrice +priceA;
                break;
            case 2:
                totalPrice = totalPrice +priceE;
                break;
            case 3:
                totalPrice = totalPrice +priceL;
                break;
            case 0:
                System.out.println("Your total is $" + totalPrice);
                return;
            default:
                System.out.println("This Option is not available, please try again!");
                counter--;
                break;
        }
        counter++;
        menu();
    }
}

public static void menu() {
    Scanner input = new Scanner(System.in);
    System.out.println("Counter: " + counter);
    System.out.println("Total Price: $" + totalPrice);

    System.out.println("-----------------------------");
    System.out.println("  (1) American    1.99       ");
    System.out.println("  (2) Espresso    2.99       ");
    System.out.println("  (3) Latte       3.99       ");
    System.out.println("-----------------------------");
    System.out.println("");
    System.out.println(" Please make a selection \n\t or enter 0 to total and quit.");
    custChoice = input.nextInt();
}

}

答案 6 :(得分:1)

实际上,您的switch语句中的情况必须检查与您已分配switch语句的变量相同的数据类型。您已经告诉切换检查custChoice,您已将其定义为整数,因此您的case语句也必须是整数。从case语句中的值中删除引号将使它们成为整数而不是字符串。

此外,在你的switch语句之前和之内的额外menu()调用也会搞砸了。

我添加了一些额外的打印语句来显示total,而您正在使用的default案例只有在其他案例都不匹配时才会显示,所以我将其更改为处理不寻常的情况。这些只是当然的建议。

希望这有帮助。

import java.util.Scanner;

public class Coffee {
    public static double priceA = 1.99;
    public static double priceE = 2.99;
    public static double priceL = 3.99;
    public static double totalPrice = 0.0;
    public static int custChoice;
    public static int counter =1;
    public static void main(String[] args) {
        while (counter <= 3) {
            menu();
            switch (custChoice) {
                case 1:
                  totalPrice += priceA;
                  show_total();
                  break;
                case 2:
                  totalPrice += priceE;
                  show_total();
                  break;
                case 3:
                  totalPrice += priceL;
                  show_total();
                  break;
                case 0:
                    show_total();
                    System.exit(0);
                default:
                    System.out.println("Please try again");
            }
            counter++;
        }
    }

    public static void show_total(){
        System.out.println("Your total is $" + totalPrice);
    }

    public static void menu() {
        Scanner input = new Scanner(System.in);
        System.out.println("Counter: " + counter);
        System.out.println("Total Price: $" + totalPrice);

        System.out.println("-----------------------------");
        System.out.println("  (1) American    1.99       ");
        System.out.println("  (2) Espresso    2.99       ");
        System.out.println("  (3) Latte       3.99       ");
        System.out.println("-----------------------------");
        System.out.println("");
        System.out.println(" Please make a selection \n\t or enter 0 to total and quit.");
        custChoice = input.nextInt();
    }
}