Skip to content

Instantly share code, notes, and snippets.

@ntub46010
Created November 17, 2019 14:09
Show Gist options
  • Save ntub46010/216485df9e9ece6ff3efb27ece3aba1d to your computer and use it in GitHub Desktop.
Save ntub46010/216485df9e9ece6ff3efb27ece3aba1d to your computer and use it in GitHub Desktop.
計算消費打折金額
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;
}
}
}
@ntub46010
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment