chapter three Java homework

 

3-1

产生两个随机数相加,由用户输入相加的结果,结果若对,则显示正确相加结果 true,else显示错误相加结果 false;

 1 package com.baidu;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Run$a {
 6 
 7     public static void main(String[] args) {
 8         int number1=(int)(System.currentTimeMillis()%1000);//显示两个随机数
 9         int number2=(int)(System.currentTimeMillis()/7%10);
10         
11         Scanner input = new Scanner(System.in);
12         
13         System.out.println("what is " +  number1 + "  + "+ number2 + " ? ");
14         
15         int answer = input.nextInt();
16         
17         System.out.println(number1 + "+" + number2 + "=" +answer + " is " + (number1+number2==answer));
18 
19     }
20 
21 }


3-2

用户输入一个整数,if是5的倍数,输出HiFive,if能被2整除,输出HiEven.

 1 package com.baidu;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Run$b {
 6 
 7     public static void main(String[] args) {
 8         Scanner input=new Scanner(System.in);
 9         System.out.println("enter an integer");
10         int number = input.nextInt();
11         
12         if(number%5==0)
13             System.out.println("HIFive");
14         
15         if(number%2==0)
16             System.out.println("HIEven");
17 
18     }
19 
20 }


3-3

产生两个整数number1和number2,ifnumber1<number2交换,提示what's number1-number2?,然后检查结果是否正确,if错误显示wrong,显示正确计算结果。

 1 package com.baidu;
 2 import java.util.Scanner;
 3 public class Run$c {
 4 
 5     public static void main(String[] args) {
 6         int number1 = (int)(Math.random()*10);
 7         int number2 = (int)(Math.random()*10);
 8         if(number1<number2) {
 9             int temp = number1;
10             number1 = number2;
11             number2 = temp;
12         }
13         System.out.println("what is " + number1 + " - " + number2 + " ? " );
14         Scanner input = new Scanner(System.in);
15         int answer = input.nextInt();
16         if(number1-number2==answer)
17             System.out.println("your answer current!");
18         else
19             System.out.println("your answer is wrong.");
20         System.out.println(number1 +" - " +number2 + " should be " + (number1 - number2));
21         }
22     }


3-4

通过以千克为单位的体重除以以米为单位的身高的平方得到BIM值。1pound=0.45359237kg    1inch=0.0254m

 1 package com.baidu;
 2 import java.util.Scanner;
 3 public class Run$d {
 4 
 5     public static void main(String[] args) {
 6         Scanner input=new Scanner(System.in);
 7         
 8         System.out.println("Enter weight in pounds: ");
 9         double weight = input.nextDouble();
10         
11         System.out.println("Enter height in inches: ");
12         double height = input.nextDouble();
13         
14         final double KILOGRAMS_PER_POUND = 0.45359237;
15         final double METERS_PRE_INCH = 0.0254;
16         
17         double weightINkilograms = weight *KILOGRAMS_PER_POUND;
18         double heightINMeters = height *  METERS_PRE_INCH;
19         double bmi = weightINkilograms/(heightINMeters*heightINMeters);
20         
21         System.out.println("BIM is " + bmi);
22         if(bmi<18.5)
23             System.out.println("underweight");
24         else if(bmi<25)
25             System.out.println("notmal");
26         else if(bmi<30)
27         System.out.println("overweight");
28         else
29             System.out.println("obese");
30 
31     }
32 
33 }

3-6

j检验用户输入一个数是否能同时被2和3整除,是否被2或3整除,是否能被2或3两者中的一个整除。

 1 package com.baidu;
 2 import java.util.Scanner;
 3 
 4 public class Run$e {
 5 
 6     public static void main(String[] args) {
 7     Scanner input = new Scanner(System.in);
 8     
 9     System.out.println("enter an interger: ");
10     int number = input.nextInt();
11     
12     if(number % 2 == 0 && number % 3 == 0)
13         System.out.println(number + " is divisible by 2 and 3.");
14     
15     if(number % 2 ==0 || number % 3 == 0)
16         System.out.println(number + " is divisible by 2 or 3.");
17     if(number % 2 ==0 ^ number % 3 == 0)
18         System.out.println(number + " is divisible by 2 or 3, but not both.");
19     
20 
21     }
22 
23 }


3-7

用户输入一个年份判断是否为闰年,被4整除而不能被100整除,或者可以被400整除,就是闰年。

 1 package com.baidu;
 2 import java.util.Scanner;
 3 public class Run$f {
 4 
 5     public static void main(String[] args) {
 6         Scanner input = new Scanner(System.in);
 7         System.out.println("enter a year");
 8         int year = input.nextInt();
 9         
10         boolean isLeapYear = (year % 4 == 0 && year % 100 !=0) || (year % 400 == 0);
11         
12         System.out.println(year + " is a leap year? " + isLeapYear);
13 
14     }
15 
16 }


3-8

程序随机产生两位数,提示用户输入一个两位数,进行判定,if输入的数匹配彩票的实际顺序,输出10000 ,if用户输入所以数字的匹配彩票的所以数字,输出3000,if用户输入一个数字匹配彩票的一个数字,输出1000.

 1 package com.baidu;
 2 import java.util.Scanner;
 3 public class Run$g {
 4 
 5     public static void main(String[] args) {
 6         int lottery = (int)(Math.random() * 100);
 7         
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("enter your lottery pick (two digits): ");
10         int guess = input.nextInt();
11         
12         int lotteryDigit1 = lottery / 10;
13         int lotteryDigit2 = lottery % 10;
14         
15         int guessDigit1 = lottery / 10;
16         int guessDigit2 = lottery % 10;
17         
18         System.out.println("the lottery number is " + lottery);
19         
20         if(guess==lottery)
21             System.out.println("exact match: you win 10000");
22         else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2)
23         System.out.println("match all digits: you win 3000");
24         else if(  guessDigit1 == lotteryDigit1
25                 ||guessDigit1 == lotteryDigit2
26                 ||guessDigit2 == lotteryDigit1
27                 ||guessDigit1 == lotteryDigit2)
28                 System.out.println("Match one digit: you win 1000");
29         else
30             System.out.println("sorry, no match");
31         
32     }
33 
34 }


3-9

用户输入年份,显示出相应的生肖

 1 package com.baidu;
 2 import java.util.Scanner;
 3 
 4 public class Run$h {
 5 
 6     public static void main(String[] args) {
 7         Scanner input = new Scanner(System.in);
 8         
 9         System.out.println("enter a year");
10         int year = input.nextInt();
11         
12         switch (year % 12) {
13         case 0: System.out.println("monkey");break;
14         case 1: System.out.println("rooster");break;
15         case 2: System.out.println("dog");break;
16         case 3: System.out.println("pig");break;
17         case 4: System.out.println("rat");break;
18         case 5: System.out.println("ox");break;
19         case 6: System.out.println("tiger");break;
20         case 7: System.out.println("rabbit");break;
21         case 8: System.out.println("dragon");break;
22         case 9: System.out.println("snake");break;
23         case 10: System.out.println("horse");break;
24         case 11: System.out.println("sheep");
25         }
26 
27     }
28 
29 }

 

posted on 2017-10-16 10:05  马文奇  阅读(130)  评论(0编辑  收藏  举报

导航