Java switch语句无法正常工作

时间:2017-03-20 17:42:54

标签: java switch-statement

我有两个类来计算ISP计划的成本并收集输入/显示成本。 ISPMain.java和ISP.java ISPMain应该显示服务计划并要求用户通过包的字母选择他们的计划,例如包a,包b或包c。我已将其设置为用户输入角色以选择计划。如果计划不是包c(无限制计划),则提示用户输入他们使用的小时数。

输入小时并以小时为单位存储后,ISP.java会计算成本,然后ISPMain.java会显示它。我的问题是我的ISP中的switch语句只显示默认值,我不知道为什么。谁能解释一下?

public class ISPMain
{

    public static void main(String[] args)
    {
        char pkg;
        double hoursUsed; 

        Scanner kb = new Scanner(System.in);

        System.out.println("The three packages offered are: ");
        System.out.println("Package A: For $9.95 per month, 10 hours of access are provided. \nAdditional hours are $2.00 per hour.");
        System.out.println("Package B: For $14.95 per month, 20 hours of access are provided. \nAdditional hours are $1.00 per hour.");
        System.out.println("Package C: For $19.95 per month, unlimited access is provided.");

        System.out.println("Please type the letter of the package you have: ");
        pkg = kb.nextLine().toUpperCase().charAt(0);

        if(pkg == 'A')
        {
            System.out.print("Enter number of hours: ");
            hoursUsed = kb.nextDouble();
        }
        else if(pkg == 'B')
        {
            System.out.print("Enter number of hours: ");
            hoursUsed = kb.nextDouble();
        }
        else if(pkg == 'C')
        {
            System.out.print("You have unlimited access! No need to enter hours used. \n");
        }


        ISP user = new ISP();  
        System.out.print("Total charges: " + user.calculateCharges());

    }
}
来自ISP.java的

switch语句:

public double calculateCharges()
{

    switch (pkg)
    {
        case 'A':
        if (hoursUsed < 10)
        {
            return 9.95;
        }

        else
        {
            return (hoursUsed - 10)*2 + 9.95;
        }

        case 'B':
        if (hoursUsed < 20)
        {
            return 14.95;
        }

        else
        {
            return (hoursUsed - 20) + 14.95;
        }  

        case 'C':
            return 19.95;

        default:
            System.out.println("Invalid input!");
            return 0;
    }


}

总结一下,我的if else工作正常,但是开关只显示&#34;输入无效&#34;

2 个答案:

答案 0 :(得分:1)

正如其他人所说,您需要将 pkg 变量放入switch语句的范围内。试试这个:

public double calculateCharges(char pkg){
 switch(pkg){
     case('A'):{
      //insert code here
       break;
     }
     case('B'):{
    //insert code here
     break;
     }
     case('C'):{
     //insert more code here
     break;
     }
  default:{
  //code here 
  break;
  }
 }
}

然后调用您的方法:

System.out.print("Total charges: " + user.calculateCharges(pkg));

答案 1 :(得分:1)

默认:case语句将始终在您的代码中运行,因为char pkg未定义为您方法的参数。

用户可以输入A,B或C,但由于未通过,因此最终结果将始终为默认值,从而导致输入无效。

尝试:

public double calculateCharges(char pkg){
    switch (pkg)
    {
       case 'A':
       if (hoursUsed < 10){
        return 9.95;
       }else{
        return (hoursUsed - 10)*2 + 9.95;
       }

       case 'B':
       if (hoursUsed < 20){
        return 14.95;
       }else{
        return (hoursUsed - 20) + 14.95;
       }  

       case 'C':
       return 19.95;

       default:
       System.out.println("Invalid input!");
       return 0;
}