3.8 计算身体质量指数
public class Demo02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个体重(以磅为单位)");
double weight = input.nextDouble();
System.out.println("输入一个身高(以英寸为单位)");
double height = input.nextDouble();
final double KILOGRAMS_PER_POUND = 0.45359237;
final double METERS_PER_INCH = 0.0254;
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters = height * METERS_PER_INCH;
double bmi = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("BMI为 " + bmi);
if (bmi < 18.5) {
System.out.println("偏瘦型");
}else if (bmi < 25) {
System.out.println("正常型");
}else if (bmi < 30) {
System.out.println("超重型");
}else {
System.out.println("过胖型");
input.close();
}
}
3.9 计算税率
//递进式开发和测试:对所有的程序都应该首先写少量代码然后再进行测试,之后再写更多的代码;这样使得调试变得很容易,因为错误很可能在你刚刚加进去的新代码中
public class Demo03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入您的身份:(0代表单身纳税人、1代表已婚或者符合条件的鳏寡、2代表已婚单独纳税人、3代表家庭户主纳税人)");
int status = input.nextInt();
System.out.println("请输入您的收入:");
double income = input.nextDouble();
//该tax必须初始化因为所有的tax赋值都在if语句中,编译器认为这些语句可能不会执行
double tax = 0;
if (status == 0) {
if (income <= 8350) {
tax = income * 0.10;
}else if (income <= 33950) {
tax = 8350 * 0.10 + (income - 8350) * 0.15;
}else if (income <= 82250) {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
}else if (income <= 171550) {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (income - 52550) * 0.28;
}else if (income <= 372950) {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (171550 - 52550) * 0.28 + (income - 171550) * 0.33;
}else {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (171550 - 52550) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
}
}else if (status == 1) {
if (income <= 16700) {
tax = income * 0.10;
}else if (income <= 67900) {
tax = 16700 * 0.10 + (income - 16700) * 0.15;
}else if (income <= 137050) {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
}else if (income <= 208850) {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
}else if (income <= 372950) {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137250 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
}else {
tax = 16700 * 0.10 + (63900 - 16700) * 0.15 + (137250 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;}
}else if (status == 2) {
if (income <= 8350) {
tax = income * 0.10;
}else if (income <= 33950) {
tax = 8350 * 0.10 + (income - 8350) * 0.15;
}else if (income <= 82250) {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
}else if (income <= 171550) {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (income - 52550) * 0.28;
}else if (income <= 372950) {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (171550 - 52550) * 0.28 + (income - 171550) * 0.33;
}else {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (171550 - 52550) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}
}else if (status == 3) {
if (income <= 8350) {
tax = income * 0.10;
}else if (income <= 33950) {
tax = 8350 * 0.10 + (income - 8350) * 0.15;
}else if (income <= 82250) {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
}else if (income <= 171550) {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (income - 52550) * 0.28;
}else if (income <= 372950) {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (171550 - 52550) * 0.28 + (income - 171550) * 0.33;
}else {
tax = 8359 * 0.10 + (33950 - 8350) * 0.15 + (52250 - 33950) * 0.25 + (171550 - 52550) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}
}else {
System.out.println("输入身份有误!");
System.exit(0);//终止该程序
}
System.out.println("总纳税额度为:" + (int)(tax * 100 + 0.5) / 100.0);
input.close();
}
}
3.11 判断闰年
//判断闰年
public class Demo01 {
//如果某年可以被4整除而不能被100整除或者能被400整除的就是闰年
//如:2012、2016、2020
//如:1600、2000
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入一个年数:");
int year = input.nextInt();
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (isLeapYear) {
System.out.println("是闰年");
}else {
System.out.println("不是闰年");
}
}
3.12 彩票
public static void main(String[] args) {
// TODO Auto-generated method stub
int lottery = (int)(Math.random() * 100);
Scanner input = new Scanner(System.in);
System.out.println("请输入两个数字:");
int guess = input.nextInt();
int lottery1 = lottery / 10;
int lottery2 = lottery % 10;
int guess1 = guess / 10;
int guess2 = guess % 10;
System.out.println("彩票数字为: " + lottery);
if (guess == lottery) {
System.out.println("您的奖金为:$10000");
}else if (guess2 == lottery1 && guess1 == lottery2) {
System.out.println("您的奖金为:$4000");
}else if (guess1 == lottery1 || guess1 == lottery2 || guess2 == lottery2) {
System.out.println("您的奖金为:$1000");
}else {
System.out.println("很抱歉,您没有得奖!");
}
}