Switch Statement不输出;

时间:2016-09-20 19:47:03

标签: java switch-statement

我的代码不会输出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;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

工作正常。请记住,只有当payPeriod String与"每周"完全相同时,它才会进入case语句。或者"双周刊",没有空格,没有大写等等。