Created
November 17, 2019 14:09
-
-
Save ntub46010/216485df9e9ece6ff3efb27ece3aba1d to your computer and use it in GitHub Desktop.
計算消費打折金額
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vincent.demo; | |
import java.util.*; | |
public class JavaMain { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.println("請輸入你的年齡"); | |
int age = scan.nextInt(); | |
System.out.println("請輸入你的消費金額"); | |
long money = scan.nextLong(); | |
double discount = getDiscountRate(age, money); | |
String discountMsg = discount == 0 | |
? "無打折" | |
: String.format("可打%.0f折", (1 - discount) * 100); | |
long actualMoney = Math.round(money * (1 - discount)); | |
System.out.printf("%s\n消費總金額:%d\n折扣後金額:%d", | |
discountMsg, money, actualMoney); | |
scan.close(); | |
} | |
private static double getDiscountRate(int age, long money) { | |
if (age == 30) { | |
return 0.05; | |
} else if (money >= 100000) { | |
return 0.1; | |
} else if (money >= 80000) { | |
return 0.05; | |
} else if (money >= 50000) { | |
return 0.02; | |
} else { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
題目來源:
https://www.facebook.com/groups/406460463268219/permalink/544173856163545/