在if语句后使用switch语句

时间:2014-09-21 17:12:51

标签: java if-statement switch-statement

我希望在switch语句后使用if语句,但我不能,我不知道问题是什么。

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.");

}
}

现在我想用switch语句告诉用户如果他/她选择了包“B”,他就可以节省20美元。

1 个答案:

答案 0 :(得分:1)

我查看了您的代码,并对 ALOT 进行了编辑和改进,我发现的主要问题是您在错误的地方使用}。我相信这是因为你没有很好地组织你的代码;将来考虑组织你的代码,以便更容易找到错误,下面我已经更正了你的代码并将最后几行放入评论中,因为我不知道为什么你有它们,如果有任何问题,请问我:

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.");
     **/
}
}