2016-09-20 61 views
-3

我的代碼不會輸出switch語句我需要使用不同的語句還是條件?直到switch語句的每一件事情都完美無缺。開關狀態不輸出;幫幫我;

import java.util.Scanner; 
public class NewClass { 
public static void main (String[]args){ 
    Scanner user= new Scanner(System.in); 
    int hours; 
    double biWeekly,payrate,weekly; 
    String payPeriod; 
    //Variables 

    System.out.print("Do you get paid weekly or bi weekly?:"); 
    payPeriod=user.nextLine(); 
    System.out.print("How many hours did you work ths week?:"); 
    hours=user.nextInt(); 
    System.out.print("How much is your pay rate?:"); 
    payrate=user.nextInt(); 
    //the code stops here and wont output the rest 

    biWeekly=(hours*2)*payrate; 
    weekly=hours*payrate; 

    switch(payPeriod){ 
     case "weekly": 
      System.out.println("Your weekly pay is $"+weekly+" Without tax reductions"); 
      break; 
     case "biweekly": 
      System.out.println("Your bi-weekly pay is $"+biWeekly+" without tax reductions"); 
      break; 

    } 
    } 

    } 
+0

步調試是您的朋友。 – sstan

+1

添加一個'default'語句來打印並看看這個值是什麼 – sidgate

+0

您是否在switch語句之前嘗試打印出payPeriod,和/或調用'payPeriod.trim()'? – Brydenr

回答

-1

它工作正常。請記住,只有當payPeriod字符串完全符合「每週」或「雙週」,沒有空格,沒有上限等情況時,纔會進入案例陳述。

+0

是有道理的,我現在看到,謝謝你 –