2014-09-21 140 views
0

我想在if語句後面使用switch語句,但我不能,也不知道是什麼問題。if語句後使用switch語句

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美元。

+0

你想在哪裏使用該開關?在最底層? – Pokechu22 2014-09-21 17:16:30

+0

你不能只加一個'if(selectPackage =='B')System.out.println(「你保存20 $」);'? – nem035 2014-09-21 17:18:25

+0

是否有錯誤,或者您希望我們爲您編寫代碼? – James 2014-09-21 17:20:43

回答

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