1 package 编程练习题chapter3;
 2 import java.util.Scanner;
 3 public class Exercise3_1 {
 4   public static void main(String[] args){
 5       Scanner input = new Scanner(System.in);
 6       System.out.print("Enter a, b, c: ");
 7       double a = input.nextDouble();
 8       double b = input.nextDouble();
 9       double c = input.nextDouble();
10       
11       double discriminant =  b * b - 4 * a *c;
12       
13       if (discriminant < 0){
14           System.out.println("The equation has no roots");          
15       } 
16       else if (discriminant == 0){
17           double r1 = -b / (2 * a);
18           System.out.println("The root is " + r1);
19       }
20       else {
21           double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
22           double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
23           System.out.println("The roots is " + r1 + " and " + r2);
24       }
25   }
26 }
 1 package 编程练习题chapter3;
 2 import java.util.Scanner;
 3 public class Exercise3_2 {
 4   public static void main(String[] args){ 
 5       Scanner input = new Scanner(System.in);
 6       
 7       System.out.print("Enter an integer: ");
 8       int number = input.nextInt();
 9       
10       System.out.println("Is " + number + " an even number? " + (number % 2 == 0));
11   }
12 }
 1 package 编程练习题chapter3;
 2 import java.util.Scanner;
 3 public class Exercise3_3 {
 4   public static void main (String[] args){
 5       Scanner input = new Scanner(System.in);
 6       System.out.print("Enter a, b, c, d, e, f: ");
 7       
 8       double a = input.nextDouble();
 9       double b = input.nextDouble();
10       double c = input.nextDouble();
11       double d = input.nextDouble();
12       double e = input.nextDouble();
13       double f = input.nextDouble();
14       double detA = a * d - b * c;
15       
16       if (detA == 0){
17           System.out.println("The equationhas no solution");
18       }
19       else {
20           double x = (e * d - b * f) / detA;
21           double y = (a * f - e * c) / detA;
22           System.out.println("x is " + x + " and y is " + y);
23       }
24   }
25 }
 1 package 编程练习题chapter3;
 2 import javax.swing.*;
 3 public class Exercise3_4 {
 4   public  static void main(String[] args){
 5       int number1 = (int)(System.currentTimeMillis() % 100);
 6       int number2 = (int)(System.currentTimeMillis() * 7 % 100);
 7       
 8       String resultString = JOptionPane.showInputDialog("What is " + number1 + " + " + number2 + "?");
 9       
10       int result = Integer.parseInt(resultString);
11       
12       JOptionPane.showConfirmDialog(null, number1 + " + " + number2 + "=" + result + " is " + (number1 + number2 == result));
13       
14   }
15 }
 1 package 编程练习题chapter3;
 2 import java.util.*;
 3 public class Exercise3_5 {
 4     public static void main (String[] args){
 5       Scanner input = new Scanner(System.in);
 6       int number1 = (int)(System.currentTimeMillis() % 10);
 7       int number2 = (int)(System.currentTimeMillis() * 7 % 10);
 8       int number3 = (int)(System.currentTimeMillis() * 3 % 10);
 9   
10       System.out.print("What is " + number1 + " + " + number2 + " + " + number3 + "?");
11       
12       int answer = input.nextInt();
13       
14       System.out.println(number1 + " + " + number2 + " + " + number3 + " = " + answer + " is " + 
15        (number1 + number2 + number3 == answer));
16     }
17 }
 1 package 编程练习题chapter3;
 2 import java.util.*;
 3 public class Exercise3_6 {
 4   public static void main(String[] args) {
 5     Scanner input = new Scanner(System.in);
 6    
 7     System.out.print("Enter weight in pounds: ");
 8     double weight = input.nextDouble();
 9     
10     System.out.print("Enter feet: ");
11     double feet = input.nextDouble();
12     
13     System.out.print("Enter inches: ");
14     double inches = input.nextDouble();
15 
16     double height = feet * 12 + inches; 
17     double bmi = weight * 0.45359237 / ((height * 0.0254) * (height * 0.0254));
18     System.out.println("Your BMI is " + bmi);
19     
20     if (bmi < 16)
21       System.out.println("You are seriously underweight");
22     else if (bmi < 18)
23       System.out.println("You are underweight");
24     else if (bmi < 24)
25       System.out.println("You are normal weight");
26     else if (bmi < 29)
27       System.out.println("You are over weight");
28     else if (bmi < 35)
29       System.out.println("You are seriously over weight");
30     else
31       System.out.println("You are gravely over weight");
32   }
33 }

 

 1 package 编程练习题chapter3;
 2 
 3 public class Exercise3_8 {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         System.out.print("Enetr three integer: ");
 7         int num1 = input.nextInt();
 8         int num2 = input.nextInt();
 9         int num3 = input.nextInt();
10         
11         if (num1 > num2) {
12             int temp = num1;
13             num1 = num2;
14             num2 = temp;
15         }
16         
17         if (num2 > num3) {
18             int temp = num2;
19             num2 = num3;
20             num3 = temp;
21         }
22         
23         if (num1 > num2) {
24             int temp = num1;
25             num1 = num2;
26             num2 = temp;
27         }
28         System.out.println("The sorted numbers are " + num1 + " " + num2 + " " + num3);
29     }
30 
31 }
 1 package 编程练习题chapter3;
 2 
 3 public class Exercise3_11 {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         
 7         System.out.print("Enter a month in the year: ");
 8         int month = input.nextInt();
 9         
10         System.out.print("Enetr a year: ");
11         int year = input.nextInt();
12         
13         int numberOfDaysInMonth = 0;
14         
15         switch(month){
16         case 1:
17             System.out.print("January " + year);
18             numberOfDaysInMonth = 31;
19             break;
20         case 2:
21             System.out.print("February " + year);
22             if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
23                 numberOfDaysInMonth = 29;
24             }
25             else {
26                 numberOfDaysInMonth = 28;
27             } 
28             break;
29         case 3:
30             System.out.print("March " + year);
31             numberOfDaysInMonth = 31;
32             break;
33         case 4:
34             System.out.print("April " + year);
35             numberOfDaysInMonth = 30;
36             break;
37         case 5:
38             System.out.print("May " + year);
39             numberOfDaysInMonth = 31;
40             break;
41         case 6:
42             System.out.print("June " + year);
43             numberOfDaysInMonth = 30;
44             break;
45         case 7: 
46             System.out.print("July " + year);
47             numberOfDaysInMonth = 31;
48             break;
49         case 8: 
50             System.out.print("August " + year);
51             numberOfDaysInMonth = 31;
52             break;
53         case 9: 
54             System.out.print("September " + year);
55             numberOfDaysInMonth = 30;
56             break;
57         case 10: 
58             System.out.print("October " + year);
59             numberOfDaysInMonth = 31;
60             break;
61         case 11: 
62             System.out.print("November " + year);
63             numberOfDaysInMonth = 30;
64             break;
65         case 12: 
66             System.out.print("December " + year);
67             numberOfDaysInMonth = 31;
68             break;
69         }
70         System.out.println(" has " + numberOfDaysInMonth + " days");
71     }
72 
73 }

 

posted on 2013-01-23 21:36  bailun  阅读(3188)  评论(1编辑  收藏  举报