Calculating a total price by using switch statement and sentinel controlled loop My homework is to make a program that calculates the total value of products sold.  It works, but I use the if statement because it was asking for the quantity on the output before ending the loop. And for the same reason, I couldn't make the program ask the user again to enter a number from 1 to 5.

import java.util.Scanner;

public class Sales {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double sum = 0;
        int n = -1 ;
        while(n !=0 ) {

            System.out.println("Enter product number 1-5 (Enter  to stop)");
            n = input.nextInt();

            if (n==0)
                break;

            System.out.println("Enter quantity of product");
            int q = input.nextInt();

            switch (n) {
                case 1:
                    sum += 2.98*q;
                    break;
                case 2:
                    sum += 4.50*q;
                    break;
                case 3:
                    sum += 9.98*q;
                    break;
                case 4:
                    sum += 4.49*q;
                    break;
                case 5:
                    sum += 6.87*q;
                    break;
                default:
                    sum += 0;
                    break;
            }
        } System.out.printf("Total cost is $%.2f",sum);
    }
}
Prefer not to use single characters as variable names (except for simple loop counters).  Saving a few keystrokes is not worth it if your readers cannot understand your code.

Based on the context, it looks like n and q should respectively be named productNumber and productQuantity.
The sum += 0 in the default looks unnecessary.  Just remove it if so, even if it's only there to have the default do something.  Keep things simple.