/*3-2.程序员:龚猛
第三章全部例题*/
1 package zhenshu; 2 import java.util.Scanner; 3 public class practise { 4 5 public static void main(String[] args) { 6 int number1 = (int)(Math.random() * 10); 7 int number2 = (int)(Math.random() * 10); 8 9 if(number1 < number2) { 10 int temp = number1; 11 number1 = number2; 12 number2 = temp; 13 } 14 System.out.print 15 ("what is" + number1 + "-" + number2 +"?"); 16 Scanner input = new Scanner(System.in); 17 int answer = input.nextInt(); 18 19 if (number1 - number2 == answer) 20 System.out.println("You are correct!"); 21 else{ 22 System.out.println("Your answer is wrong."); 23 System.out.println(number1 + "-" + number2 + "should be" + (number1 - number2 )); 24 } 25 } 26 27 }
/*3-3.*/
1 package zhenshu; 2 import java.util.Scanner; 3 public class Demo { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner (System.in); 7 System.out.println("Enter an integer:"); 8 int number = input.nextInt(); 9 10 if (number % 5 ==0) 11 System.out.println("HiFive"); 12 13 if (number % 2 ==0) 14 System.out.println("HiEven"); 15 16 } 17 18 }
/*3-3*/
1 package zhenshu; 2 import java.util.Scanner; 3 public class bmi { 4 5 private static final char[] Obese = null; 6 7 public static void main(String[] args) { 8 Scanner input = new Scanner(System.in); 9 10 System.out.print("Enter height in pounds:"); 11 double weight = input.nextDouble(); 12 13 System.out.print("Enter height in inches:"); 14 double height = input.nextDouble(); 15 16 final double KILOGRAMS_PER_POUND = 0.45359237; 17 final double METERS_PER_INCH = 0.0254; 18 19 double weightInKilograms = weight * KILOGRAMS_PER_POUND; 20 double heightInMeters = height * METERS_PER_INCH; 21 double bmi = weightInKilograms / (heightInMeters * heightInMeters); 22 23 System.out.println("BMI is" + bmi); 24 if (bmi < 18.5) 25 System.out.println("Underweight"); 26 else if (bmi < 25) 27 System.out.println("Normal"); 28 else if (bmi < 30) 29 System.out.println("overweight"); 30 else 31 System.out.println(Obese); 32 33 34 } 35 36 }