第三章 选择
3.8 计算身体质量指数
1 package com.chapter3;
2
3 import java.util.Scanner;
4
5 public class ComputeAndInterpretBMI {
6
7 /**
8 *计算身体质量指数
9 *BMI
10 *BMI<18.5 偏瘦
11 *18.5<=BMI<25.0 正常
12 *25.0<=BMI<30.0 超重
13 *30.<=BMI 过胖
14 */
15
16 public static void main(String[] args) {
17 Scanner input=new Scanner(System.in);
18
19 System.out.println("输入您的体重(英镑):");
20 double weight=input.nextDouble();
21
22 System.out.println("输入您的身高(英寸):");
23 double height=input.nextDouble();
24
25 final double KILOGRAMS_PER_POUND=0.45359237;
26 final double METERS_PER_INCH=0.0254;
27
28 double weightInKilograms=weight*KILOGRAMS_PER_POUND;
29 double heightInMeters=height*METERS_PER_INCH;
30 double bmi=weightInKilograms/(heightInMeters*heightInMeters);
31
32 System.out.println("BMI is"+bmi);
33 if(bmi<18.5){
34 System.out.println("偏瘦");
35 }else if(bmi<25){
36 System.out.println("正常");
37 }else if(bmi<30){
38 System.out.println("超重");
39 }else{
40 System.out.println("过胖");
41 }
42
43 }
44
45 }
3.9 计算税率
1 package com.chapter3;
2
3 import java.util.Scanner;
4
5 public class ComputeTax {
6 /**
7 * 计算单身纳税人税率
8 */
9
10 public static void main(String[] args) {
11
12 Scanner input=new Scanner(System.in);
13
14 System.out.println("0-单身纳税人,1-已婚共同纳税人,2-已婚单独纳税人,3-家庭户主纳税人,请选择符合您的身份:");
15
16 int status=input.nextInt();//status:身份
17
18 System.out.println("输入应纳税所得额:");
19
20 double income=input.nextDouble();
21
22 double tax=0;
23
24 if(status==0){
25 if(income<=8350){
26 tax=income*0.10;
27 }else if(income<=33950){
28 tax=8350*0.10+(income-8350)*0.15;
29 }else if(income<=82250){
30 tax=8350*0.10+(33950-8350)*0.15+(income-33950)*0.25;
31 }else if(income<=171550){
32 tax=8350*0.10+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28;
33 }else if(income<=372950){
34 tax=8350*0.10+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(income-171550)*0.33;
35 }else{
36 tax=8350*0.10+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(372950-171550)*0.33+(income-372950)*0.35;
37 }
38 }
39 System.out.println("Tax is"+tax);
40 }
41 }
3.10 逻辑操作符
1 package com.chapter3;
2
3 import java.util.Scanner;
4
5 public class TestBooleanOperators {
6 /**
7 *检验一个数:
8 *1.是否能同时被2和3整除
9 *2.是否2或3整除
10 *3.是否只能被2或3两者之间的一个整除
11 */
12
13 public static void main(String[] args) {
14
15 Scanner input=new Scanner(System.in);
16
17 System.out.println("输入一个整数:");
18
19 int number=input.nextInt();
20
21 if(number%2==0 && number%3==0){
22 System.out.println("这个数能被2和3整除");
23 }
24 if(number%2==0 || number%3==0){
25 System.out.println("这个数能被2或3整除");
26 }
27 if(number%2==0 ^ number%3==0){
28 System.out.println("这个数只能被2或3两者之间的一个整除");
29 }
30 }
31 }