Java语言程序设计-基础篇-第八版-第四章

时间:2022-07-01 01:55:44
 1 package chapter4;
 2 
 3 public class FutureTuition {
 4     public static void main(String[] args){
 5         double tuition = 10000;
 6         int year = 1;
 7         while (tuition < 20000){
 8             tuition = tuition * 1.07;
 9             year++;
10         }
11     System.out.println("Tuition will be doubled in " + year + " years");    
12     }
13 }
 1 package chapter4;
 2 //求最大公约数;
 3 public class GreatestCommonDivisor {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         System.out.print("Enter first integer: ");
 7         int n1 = input.nextInt();
 8         System.out.print("Enter second integer: ");
 9         int n2 = input.nextInt();
10         
11         int gcd = 1;
12         int k = 2;
13         while (k <= n1 && k <= n2){
14             if (n1 % k == 0 && n2 % k == 0)
15                 gcd = k;
16                 k++;
17         }
18         System.out.println("The greatest common divisor for " + n1 + " and " + n2 +
19                 " is " + gcd);
20     }
21 
22 }
 1 package chapter4;
 2 
 3 public class GuessNumber {
 4   public static void main(String[] args) {
 5     java.util.Scanner input = new java.util.Scanner(System.in);
 6     int number = (int)(Math.random() * 101);   
 7     
 8     System.out.println("Guess a magic number between 0 and 100");
 9     int guess = -1; 
10     while (guess != number) {
11       System.out.print("\nEnter your guess: ");
12       guess = input.nextInt();
13 
14       if (guess == number)
15         System.out.println("Yes, the number is " + number);
16       else if (guess > number)
17         System.out.println("Your guess is too high");
18       else
19         System.out.println("Your guess is too low");
20     } 
21   }
22 }
 1 package chapter4;
 2 
 3 public class GuessNumberOneTime {
 4     public static void main(String[] args){
 5         int number = (int)(Math.random() * 101);
 6         java.util.Scanner input = new java.util.Scanner(System.in);
 7         System.out.println("Guess a magic number between 0 and 100");
 8         System.out.print("\nEnter your guess: ");
 9         int guess = input.nextInt();
10         
11         if (guess == number)
12             System.out.println("Yes, the number is " + number);
13         else if (guess > number)
14             System.out.println("Your guess is too high");
15         else
16             System.out.println("Your guess is too low");
17     }
18 }
 1 package chapter4;
 2 
 3 public class GuessNumberUsingBreak {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         int number = (int)(Math.random() * 101);
 7         System.out.println("Guess a magic number between 0 and 100");
 8         
 9         while (true){
10             System.out.print("\nEnter your guess: ");
11             int guess = input.nextInt();
12             
13             if (guess == number){
14                 System.out.println("Yes, the number is " + number);
15                 break;
16             }
17             else if (guess > number)
18                 System.out.println("Your guess is too high");
19             else
20                 System.out.println("Your guess is too low");
21             
22         }    
23     }
24 }
 1 package chapter4;
 2 
 3 public class MonteCarloSimulation {
 4     public static void main(String[] args){
 5         final int NUMBER_OF_TRIALS = 10000000;
 6         int numberOfHits = 0;
 7         
 8         for (int i = 0; i < NUMBER_OF_TRIALS; i++){
 9             double x = Math.random() * 2.0 - 1;
10             double y = Math.random() * 2.0 - 1;
11             if (x * x + y * y <= 1)
12                 numberOfHits++;
13             
14         }
15         double pi = 4.0 * numberOfHits / NUMBER_OF_TRIALS;
16         System.out.println("PI is " + pi);
17     }
18 
19 }
 1 package chapter4;
 2 
 3 public class MultiplicationTable {
 4     public static void main(String[] args){
 5         System.out.print("        Multiplication Table\n");
 6         System.out.print("    ");
 7         for (int j = 1; j <= 9; j++)
 8             System.out.print("   " + j );
 9         
10         System.out.println("\n---------------------------------------------");
11         for (int i = 1; i <= 9; i++){
12             System.out.print(i + " | ");
13             for (int j = 1; j <= 9; j++){
14         System.out.printf("%4d", i * j);
15         }
16         System.out.println();
17     }
18   }
19 }
 1 package chapter4;
 2 //求素数(素数是指只能被1和它本身整除的数);
 3 public class PrimeNumber {
 4     public static void main(String[] args){
 5         final int NUMBER_OF_PRIMES = 50;
 6         final int NUMBER_OF_PRIMES_PER_LINE= 10;
 7         int count = 0;
 8         int number = 2;
 9         
10         System.out.println("The first 50 prime numbers are \n");
11         
12         while (count < NUMBER_OF_PRIMES){
13                  boolean isPrime = true;
14                  for(int divisor = 2; divisor <= number / 2; divisor++){
15                      if (number % divisor == 0){
16                          isPrime = false;
17                          break;
18                      }
19                  }
20                  if (isPrime) {
21                      count++;
22                      if (count % NUMBER_OF_PRIMES_PER_LINE == 0){
23                          System.out.println(number);
24                      }
25                      else
26                          System.out.print(number + " "); 
27                  }
28                  number++;
29         }
30     }
31 
32 }
 1 package chapter4;
 2 
 3 public class SentinelValue {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         System.out.print("Enter an int value: ");
 7         int data = input.nextInt();
 8         
 9         int sum = 0;
10         while (data != 0){
11             sum += data;
12             System.out.print("Enter an int value: ");
13             data = input.nextInt();
14         }
15         System.out.println("The sum is " + sum);
16     }
17     
18 
19 }
 1 package chapter4;
 2 
 3 public class SubtractionQuizLoop {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         
 7         final int NUMBER_OF_QUESTIONS = 5;
 8         int correctCount = 0;
 9         int count = 0;
10         long startTime = System.currentTimeMillis();
11         String output = "";
12         
13         while (count < NUMBER_OF_QUESTIONS){
14             int num1 = (int)(Math.random() * 10);
15             int num2 = (int)(Math.random() * 10);
16             
17             if (num1 < num2){
18                 int temp = num1;
19                 num1 = num2;
20                 num2 = temp;
21             }
22             
23             System.out.print("What is " + num1 + " - " + num2 + " ? ");
24             int answer = input.nextInt();
25             
26             if (num1 - num2 == answer){
27                 System.out.println("You are correct!");
28                 correctCount++;
29             }
30             else 
31                 System.out.println("You answer is wrong.\n" + num1 + " - " + 
32                 num2 + " shoule be " + (num1 - num2));
33             count++;
34             
35             output += "\n" + num1 + "-" + num2 + "=" + answer + ((num1 - num2 == answer) ? " correct" : " wrong");
36         }
37         long endTime = System.currentTimeMillis();
38         long testTime = endTime - startTime;
39         System.out.println("Correct count is " + correctCount + 
40         "\nTest time is " + testTime / 1000 + " second\n" + output);
41     }
42 }
 1 package chapter4;
 2 
 3 public class TestBreak {
 4     public static void main(String[] args){
 5         int sum = 0;
 6         int number = 0;
 7         while (number < 20){
 8             number++;
 9             sum += number;
10             if (sum >= 100) break;    
11         }
12         
13         System.out.println("The numeber is " + number);
14         System.out.println("The sum is " + sum);
15         
16     }
17  
18 }
 1 package chapter4;
 2 
 3 public class TestContinue {
 4     public static void main(String[] args){
 5         int sum = 0;
 6         int number = 0;
 7         
 8         while (number < 20){
 9             number++;
10             if (number == 10 || number == 11) continue;
11             sum += number;
12         }
13         System.out.println("The sum is " + sum);
14     }
15 
16 }
 1 package chapter4;
 2 
 3 public class TestDoWhile {
 4     public static void main(String[] args){
 5         int data;
 6         int sum = 0;
 7         java.util.Scanner input = new java.util.Scanner(System.in);
 8         
 9         do {
10             System.out.print("Enter an int value: ");
11             data = input.nextInt();
12             sum += data;
13         }while(data != 0);
14         
15         System.out.println("The sum is " + sum);
16         
17     }
18 
19 }
 1 package chapter4;
 2 
 3 public class TestSum {
 4     public static void main (String[] args){
 5         float sum = 0;
 6         for (float i = 0.01f; i <= 1.0f; i = i + 0.01f)
 7             sum += i;
 8         System.out.println("The sum is " + sum);
 9     }
10 }