1. 程式人生 > >全域性 常量定義和字串的一些比較

全域性 常量定義和字串的一些比較

這字串其中有個小技巧就是忽略大小寫

import java.util.Scanner;
public class Test2{
//全域性 常量
//static:因為 main是static,而靜態方法只能訪問靜態成員
//final:最終 定義常量
//public 公開被大家訪問的
//private 私有的只能被這個類訪問
public static final int SINGLE = 1;
public static final int MARRIED = 2;

private static final double RATE1=0.15;
private static final double RATE2=0.28;
private static final double RATE3=0.31;
private static final double SINGLE_BRACKET1 = 21450;
private static final double SINGLE_BRACKET2 = 51900;

private static final double MARRIED_BRACKET1 = 35800;
private static final double MARRIED_BRACKET2 = 86500;

public static void main(String []agrs){
Scanner in = new Scanner(System.in);
System.out.println("請輸入您的收入:");
double income =in.nextDouble();
in.nextLine();

System.out.println("請輸入婚姻狀態(s 單身/ M 已婚):");
String input = in.nextLine();
//in.nextLine();
int status =0;
//不管大小寫比較
if(  "s".equalsIgnoreCase(input)){
status=SINGLE;
}else if("M".equalsIgnoreCase(input)){
status =MARRIED;
}else{
System.out.println("Bad input");
return;
}
double tax =0;
if(status ==SINGLE){
if(income<= SINGLE_BRACKET1){
tax = RATE1* income;
}else if( income <=SINGLE_BRACKET2){
tax = RATE1* SINGLE_BRACKET1+RATE2*( income- SINGLE_BRACKET1);
}else{
tax = RATE1* SINGLE_BRACKET1+RATE2*( SINGLE_BRACKET2- SINGLE_BRACKET1)+RATE3*( income- SINGLE_BRACKET2);
}
}else{
if(income<= MARRIED_BRACKET1){
tax = RATE1* income;
}else if( income <=MARRIED_BRACKET2){
tax = RATE1* MARRIED_BRACKET1+RATE2*( income- MARRIED_BRACKET1);
}else{
tax = RATE1* MARRIED_BRACKET1+RATE2*( MARRIED_BRACKET2- MARRIED_BRACKET1)+RATE3*( income- MARRIED_BRACKET2);
}
}
System.out.println("應交的稅為:"+tax);
}

}