kimimi
kimimi

Reputation: 13

Use switch statement after if statement

I want to use switch statement after an if statement but I can't and I don't know what is the problem.

public class main {

    public static void main (String[] args) {

        String input; //To hold user's input.
        char selectPackage; //To hold Internet Package
        double hourUsage, totalCharges, addCharges; //other variables

        //Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        //Prompt the user to select a Internet Package.
        System.out.print("Which package did you purchase? ( Enter the Package's letter)");
        input = keyboard.nextLine();
        selectPackage = input.charAt(0);

        System.out.print("Please select the amount of hours used.");
        input = keyboard.nextLine();
        hourUsage = Double.parseDouble(input);

        //Display pricing for selected package...
        switch (selectPackage)
        {
            case 'a':
            case 'A':
                if (hourUsage > 10)
                {
                    addCharges = hourUsage - 10;
                    totalCharges = (addCharges * 2.0) + 9.95;
                    System.out.println("You have used " + hourUsage + " hours and your total is $" + 
                    totalCharges + " per month. ");
                }
                else
                    System.out.println("Your total is $9.95 per month.");
                break;

            case 'b':
            case 'B':
                if (hourUsage > 20 )
                { 
                    addCharges = hourUsage - 20;
                    totalCharges = (addCharges * 1.0) + 13.95;
                    System.out.println("You have used " + hourUsage + " and your total is $" + totalCharges + " per month.");
                }    
                else
                    System.out.println("Your total is $13.95 per month.");
                break;

            case 'c':
            case 'C':
                System.out.println("Your total is $19.95 per month.");
                break;

            default:
                System.out.println("Invalid Choice. Choice A,B,C");
        }
    }
}

System.out.println("Your total is $19.95 per month.");

}
else
     System.out.println("Your total is $19.95 per month.");

}
}

Now I want to use the switch statement for telling user that if he/she chose package "B", he would save 20 dollars.

Upvotes: 0

Views: 108

Answers (1)

James
James

Reputation: 338

I have had a look through your code and have made ALOT of edits and improvements, the main issue I found was your use of } in the wrong places. I believe this was because you haven't organised your code very well; in future consider organising your code to make it easier to find errors, below I have corrected your code and have put the last few lines into a comment as I'm not sure why you have them there, if there are any questions about it, just ask me:

public class Test {
public static void main(String[] args) {
    char selectPackage; //To hold Internet Package
    double hourUsage, totalCharges, addCharges; //other variables
    //Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);
    //Prompt the user to select a Internet Package.
    System.out.print("Which package did you purchase? ( Enter the Package's letter)");
    char input = keyboard.next().charAt(0);
    selectPackage = Character.toUpperCase(input);
    System.out.print("Please select the amount of hours used.");
    hourUsage = keyboard.nextDouble();
    //Display pricing for selected package...
    switch (selectPackage) {
        case 'A':
            if (hourUsage > 10) {
                addCharges = hourUsage - 10;
                totalCharges = (addCharges * 2.0) + 9.95;
                System.out.println("You have used " + hourUsage + " hours and your total is $" + totalCharges + " per month. "); 
            }
            else {
                System.out.println("Your total is $9.95 per month.");
            }
        break;
        case 'B':
            if (hourUsage > 20 ) { 
                addCharges = hourUsage - 20;
                totalCharges = (addCharges * 1.0) + 13.95;
                System.out.println("You have used " + hourUsage + " and your total is $" + totalCharges + " per month.");
            }
            else{ 
                System.out.println("Your total is $13.95 per month.");
            } 
        break;
        case 'C':
            System.out.println("Your total is $19.95 per month.");
        break;
        default:
            System.out.println("Invalid Choice. Choice A,B,C");
    }
    /**System.out.println("Your total is $19.95 per month.");
     System.out.println("Your total is $19.95 per month.");
     **/
}
}

Upvotes: 1

Related Questions