1 package 编程练习题chapter6;
2 import java.util.Scanner;
3 public class Exercise6_01Extra {
4 public static void main(String[] args){
5 Scanner input = new Scanner(System.in);
6 boolean[] isCovered = new boolean[99];
7
8 int number = input.nextInt();
9 while (number != 0){
10 if(number >= 0 && number <= 99)
11 isCovered[number -1] = true;
12 number = input.nextInt();
13 }
14
15 boolean allCovered = true;
16 for (int i = 0; i < 99; i++)
17 if (!isCovered[i]) {
18 System.out.print((i + 1) + " ");
19 allCovered = false;
20 }
21
22 if (allCovered)
23 System.out.println("The tickets cover all numbers.");
24 else
25 System.out.print(" are the missing numbers.");
26 }
27 }
1 package 编程练习题chapter6;
2
3 public class Exercise6_1 {
4 public static void main(String[] args){
5 java.util.Scanner input = new java.util.Scanner(System.in);
6 System.out.print("Enter number of students: ");
7 int numberOfStudents = input.nextInt(); //获得stu数目.
8
9 int[] scores = new int[numberOfStudents];//声明一个int型的scores数组,内容为numberOfStudents.
10 int best = 0;
11 char grade;
12
13 System.out.print("Enter " + numberOfStudents + " scores: ");
14 for (int i = 0; i < scores.length; i++) { //i = 0~3
15 scores[i] = input.nextInt(); //分别存入scores[0]~scores[3]
16
17 if (scores[i] > best)
18 best = scores[i]; //scores[i]大于0正确的输入值,则将值给best.
19 }
20
21 String output = "";
22
23 for (int i = 0; i < scores.length; i++) {
24 if (scores[i] >= best - 10)
25 grade = 'A';
26 else if (scores[i] >= best - 20)
27 grade = 'B';
28 else if (scores[i] >= best - 30)
29 grade = 'C';
30 else if (scores[i] >= best - 40)
31 grade = 'D';
32 else
33 grade = 'F';
34
35 output += "Student " + i + " score is " +
36 scores[i] + " and grade is " + grade + "\n";
37 }
38
39 System.out.println(output);
40 }
41 }
1 package 编程练习题chapter6;
2
3 public class Exercise6_2 {
4 public static void main(String[] args){
5 java.util.Scanner input = new java.util.Scanner(System.in);
6 System.out.print("Read a number: ");
7
8 int[] num = new int[10];
9
10 for (int i = 0; i < 10; i++) {
11 num[i] = input.nextInt();
12 }
13
14 for (int i = 9; i >= 0; i--) {
15 System.out.print(num[i] + " ");
16 }
17 }
18
19 }
1 package 编程练习题chapter6;
2
3 import java.util.Scanner;
4
5 public class Exercise6_3 {
6 public static void main (String[] args) {
7 Scanner input = new Scanner(System.in);
8 int[] counts = new int[100];
9
10 System.out.print("Enter the integers between 1 and 100: ");
11 int number = input.nextInt();
12
13 while (number != 0) {
14 counts[number - 1]++;
15 number = input.nextInt();
16 }
17
18 for (int i = 1; i < 100; i++) {
19 if (counts[i] > 0)
20 System.out.println((i + 1) + " occurs " + counts[i]
21 + ((counts[i] == 1) ? " time" : " times"));
22 }
23 }
24 }
1 package 编程练习题chapter6;
2
3 public class Exercise6_4 {
4 public static void main(String[] args){
5 java.util.Scanner input = new java.util.Scanner(System.in);
6
7 double[] scores = new double[10];
8 double sum = 0;
9 int count = 0;
10
11 do{
12 System.out.print("Enter a new score: ");
13 scores[count] = input.nextDouble();
14 if (scores[count] >= 0)
15 sum += scores[count];
16 } while (scores[count++] >= 0);
17
18 System.out.println("count is " + count);
19 double average = (sum) / (count - 1);
20
21 int numOfAbove = 0;
22 int numOfBelow = 0;
23 for (int i = 0; i < count - 1; i++)
24 if (scores[i] >= average)
25 numOfAbove++;
26 else
27 numOfBelow++;
28
29 System.out.println("Average is " + average);
30 System.out.println("Number of scores above or equal to the average " + numOfAbove);
31 System.out.println("Number of scores below the average " + numOfBelow);
32 }
33 }
1 package 编程练习题chapter6;
2
3 public class Exercise6_5 {
4 public static void main(String[] args){
5 int[] numbers = new int[3];
6 int numberOfDistinctValues = 0;
7
8 java.util.Scanner input = new java.util.Scanner(System.in);
9 System.out.print("Enter " + numbers.length + " numbers: ");
10
11 for (int i = 0; i < numbers.length; i++) {
12 int value = input.nextInt();
13
14 boolean isInTheArray = false;
15
16 for (int j = 0; j < numberOfDistinctValues; j++) {
17 if (numbers[j] == value) {
18 isInTheArray = true;
19 }
20 else
21 isInTheArray = false;
22 }
23
24 if (isInTheArray == false) {
25 numbers[numberOfDistinctValues] = value;
26 numberOfDistinctValues++;
27 }
28
29 }
30
31 System.out.println("The number of distinct values is " +
32 numberOfDistinctValues);
33 for (int i = 0; i < numberOfDistinctValues; i++)
34 System.out.print(numbers[i] + " ");
35 }
36
37 }
1 package 编程练习题chapter6;
2
3 public class Exercise6_7 {
4 public static void main(String[] args) {
5 int[] counts = new int[10];
6
7 for (int i = 0; i < 100; i++) {
8 int value = (int)(Math.random() * 10);
9 counts[value]++;
10 }
11
12 for (int i = 0; i < counts.length; i++) {
13 System.out.println("Counts for " + i + " is " + counts[i]);
14 }
15 }
16 }
1 package 编程练习题chapter6;
2
3 public class Exercise6_8 {
4 public static void main (String[] args) {
5 int[] list1 = {1, 2, 3, 4, 5, 6};
6 double[] list2 = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
7
8 System.out.println(average(list1));
9 System.out.println(average(list2));
10 }
11
12 public static int average(int[] array) {
13 int sum = 0;
14 for (int i = 0; i < array.length; i++) {
15 sum += array[i];
16 }
17
18
19 return sum / array.length;
20 }
21
22 public static double average(double[] array) {
23 double sum = 0;
24 for (int i = 0; i < array.length; i++) {
25 sum += array[i];
26 }
27
28
29 return sum / array.length;
30 }
31 }
package 编程练习题chapter6;
public class Exercise6_9 {
public static void main (String[] args) {
double[] numbers = new double[10];
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter 10 double numbers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextDouble();
}
System.out.println("The min is " + min(numbers));
}
public static double min (double[] list) {
double min = list[0];
for (int i = 1; i < list.length; i++)
if (list[i] < min) min = list[i];
return min;
}
}
1 package 编程练习题chapter6;
2
3 public class Exercise6_10 {
4 public static void main (String[] args) {
5 double[] numbers = new double[10];
6
7 java.util.Scanner input = new java.util.Scanner(System.in);
8 System.out.print("Enter 10 numbers: ");
9
10 for (int i = 0; i < numbers.length; i++) {
11 numbers[i] = input.nextDouble();
12 }
13 System.out.println("The indes of the min is " +
14 indexOfSmallestElement(numbers));
15 }
16
17 public static int indexOfSmallestElement(double[] list) {
18 double min = list[0];
19 int minIndex = 0;
20
21 for (int i = 0; i < list.length; i++)
22 if (list[i] < min) {
23 min = list[i];
24 minIndex = i;
25 }
26
27 return minIndex;
28 }
29 }