Need help on If-else statement with keyboard scanner logic

Viewed 56
import java.util.Scanner;

public class Section3_Projects {
    public static void main (String[] args){
        Scanner keyboard = new Scanner(System.in);
        int Package;
        int Course;
        int Total;

        System.out.println("Which of the packages do you want? \t "  +"1,2, or 3?");
        Package = keyboard.nextInt();

        System.out.println("How many courses did you enroll in this month?");
        Course = keyboard.nextInt();



        if (Package == 1 && Course <= 2) {
            System.out.println("Total cost is " + "$10");
        }
            else  {
                System.out.println("Total cost is " + "$" + (10+ (6 * (Course-2))));
                //If-else 1
            }


        if (Package == 2 && Course <= 4) {
            System.out.println("Total cost is " + "$12");
        }
            else {
                System.out.println("Total cost is " + "$" +(12 + (4 * (Course-4))));
            }//If-else 2



        if(Package == 3 && Course <= 6) {
            System.out.println("Total cost is " + "$15");
        }
            else {
                System.out.println("Total cost is " + "$" +(15+(3*(Course-6))));
        }//If-else 3


    }//end main
}

Hi all, first post on this forum and hoping someone code help me with my a work problem below. When I ran this code with the following input, I get three "Total cost" coming out:

Input/Output

Which of the packages do you want?   1,2, or 3?
3
How many courses did you enroll in this month?
10
Total cost is $58
Total cost is $36
Total cost is $27

I think I am missing something so that only one correct "Total cost" should be showing. Any help is appreciated. Thanks!

1 Answers

You have 3 outputs because you have 3 else cases which will always execute, I think you could try this:

if (Package == 1) {
    if(Course <= 2){
         System.out.println("Total cost is " + "$10");
    }else{
         System.out.println("Total cost is " + "$" + (10+ (6 * (Course-2))));
    }
}


if (Package == 2 ) {
     if(Course <= 4){
         System.out.println("Total cost is " + "$12");
     }else{
         System.out.println("Total cost is " + "$" +(12 + (4 * (Course-4))));
     }
}

if(Package == 3 ) {
    if(Course <= 6){
        System.out.println("Total cost is " + "$15");
    }else{
        System.out.println("Total cost is " + "$" +(15+(3*(Course-6))));
    }
}

The following switch statement would have the same result, but, as mentioned in the comments, it also handles the case where Package != {1,2,3}:

switch(Package){
    case 1:
        if(Course <= 2){
             System.out.println("Total cost is " + "$10");
        }else{
             System.out.println("Total cost is " + "$" + (10+ (6 * (Course-2))));
        }
        break;
    case 2:
        if(Course <= 4){
             System.out.println("Total cost is " + "$12");
        }else{
             System.out.println("Total cost is " + "$" +(12 + (4 * (Course-4))));
        }
        break;
    case 3:
        if(Course <= 6){
             System.out.println("Total cost is " + "$15");
        }else{
             System.out.println("Total cost is " + "$" +(15+(3*(Course-6))));
        }
        break;
    default:
        System.out.println("Invalid package number");
 }
Related