ourweakness ourweakness - 1 month ago 11
Java Question

adding to a double in a switch statement

I'm creating a switch wrapped in a while, for a homework assignment. Everything works except the adding to the

totalPrice
double, I've tried using

totalPrice += priceA;
and
totalPrice = totalPrice + priceA;


Here's my dump, I know it's something super simple, but my googling and documentation have turned up nil.

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


Help my StackOverflow! You're my only hope.

Answer

you can use the default-case for some basic input-error-handling, since the Code it is containing will be executed when neither of the other cases(0,1,2,3) was detected.

so, as others have mentioned, move the summation of the total price to case 0 and end that case with a return-statement which will end the execution of your program.

Further more insert some warning-text for the user in your default-case and tell that the Input was wrong. To prevent logic-issues you should decrement Counter by one, since the user hasn't bought anything in this Iteration of the while-loop and it will be incremented by one at the end of the Loop

edit: I think the answer from Dániel Kis is already really good, but has no content for the default-case. I took his solution and expanded it:

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

}

Comments