DecimalFormat df = new DecimalFormat("0.00");
System.out.println("請輸入您的商品價格");
int price = new Scanner(System.in).nextInt() ;
if (price<=1000) {
double money = price;
System.out.println("您商品的價格為:"+df.format(money));
}else {
double money = (price-1000)*0.8+1000;
System.out.println("您商品的價格為:"+df.format(money));
}
此程式碼能順利運行,假設輸入的數為1000,因為此數值未超過1000,所以會跑到price<1000的if程式內,所以輸出結果為=>您商品的價格為:1000.00;若輸入的數為2600,因為數值超過1000,會跑到else的程式內,所以會輸出=>您商品的價格為:2280.00。
DecimalFormat df = new DecimalFormat("0.00"); //DecimalFormat這套件功能是讓輸出的數字自動4捨5入到設定的小數點後幾位
System.out.println("請輸入您的收入");
int income = new Scanner(System.in).nextInt() ;
if (income<=300000) {
double tax = income*0.06;
System.out.println("您所需繳的稅金為:"+df.format(tax));
}else {
double tax = (income-300000)*0.13+18000; //+18000是因為超過的部分才要乘以0.13,原本300000的部分一樣是乘以0.6,故簡略計算
System.out.println("您需繳的稅金為:"+df.format(tax));
}
此程式碼能順利運行,假設輸入的值為300000,因為其<=300000,所以會跑到if的程式內,輸出的結果=>您所需繳的稅金為:18000.00,若輸入的數值為462000,則輸出的結果=>您需繳的稅金為:39060.00。
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("請輸入您的商品價格");
int price = new Scanner(System.in).nextInt() ;
if (price>=5000) {
double money = price*0.7;
System.out.println("您商品的價格為:"+df.format(money));
}else if (price>=4000 & price<5000) {
double money = price*0.8;
System.out.println("您商品的價格為:"+df.format(money));
}else if (price>=3000 & price<4000) {
double money = price*0.9;
System.out.println("您商品的價格為:"+df.format(money));
}else {
double money = price;
System.out.println("您商品的價格為:"+df.format(money));
}
此程式碼能順利運行,假設輸入的值為8000,因為其>=5000,所以會跑到if的程式內,輸出結果為=>您商品的價格為:5600.00,若輸入數值為4580,因其>=4000且小於5000,所以會跑到else if (price>=4000 & price<5000)的程式內,輸出結果為=>3664.00,後續就不一一說明結果。