JAVA语言程序设计基础篇(Chapter 7)课后习题参考答案

1. (简答题)(Assign grades) Write a program that reads student scores, gets the best score,  and then assigns grades based on the following scheme: 

    Grade is A if score is larger than or euqal to best - 10 

    Grade is B if score is larger than or euqal to  best - 20; 

    Grade is C if score is larger than or euqal to  best - 30; 

    Grade is D if score is larger than or euqal to  best - 40; 

    Grade is F otherwise. 

The program prompts the user to enter the total number of students, then prompts  the user to enter all of the scores, and concludes by displaying the grades. Here  is a sample run:

    Enter the number of students: 4 

    Enter 4 scores: 40 55 70 58 

    Student 0 score is 40 and grade is C 

    Student 1 score is 55 and grade is B 

    Student 2 score is 70 and grade is A 

    Student 3 score is 58 and grade is B

import java.util.Scanner;

public class Demo8 {
    public static void main(String[] args) {
        System.out.println("Enter the number of students: ");
        Scanner sc = new Scanner(System.in);
        int studentNum = sc.nextInt();
        System.out.println("Enter " + studentNum + " scores:");
        int[] score = new int[studentNum];
        for (int i = 0; i < studentNum; i++) {
            score[i] = sc.nextInt();
        }
        int best = best(score);
        for (int i = 0; i < studentNum; i++) {
            System.out.println("Student " + i + " score is " + score[i] + " and grade is " + grade(i, best, score));
        }
    }

    public static char grade(int num, int best, int[] arr) {
        if (arr[num] >= best - 10) {
            return 'A';
        } else if (arr[num] >= best - 20) {
            return 'B';
        } else if (arr[num] >= best - 30) {
            return 'C';
        } else if (arr[num] >= best - 40) {
            return 'D';
        } else {
            return 'F';
        }
    }

    public static int best(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }
}

 

2. (简答题)(Count occurrence of numbers) Write a program that reads the integers between 1  and 100 and counts the occurrences of each. Assume the input ends with 0. Here  is a sample run of the program:

Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 

2 occurs 2 times 

3 occurs 1 time 

4 occurs 1 time 

5 occurs 2 times 

6 occurs 1 time 

23 occurs 1 time 

43 occurs 1 time

import java.util.Arrays;
import java.util.Scanner;

public class Demo9 {
    public static void main(String[] args) {
        int [] num = createArrays();
        Arrays.sort(num);
        for (int i = 0; i < num.length; i++) {
            if(i==0||num[i]!=num[i-1]){
                System.out.println(num[i]+" occurs "+getCount(num[i],num)+" times");
            }
        }

    }
    public static int[] createArrays(){
        System.out.println("Enter the integers between 1 and 100:");
        Scanner sc = new Scanner(System.in);
        int [] arr1 = new int[100];
        int i = 0;
        while (true){
            arr1[i]=sc.nextInt();
            if (arr1[i]==0){
                break;
            }
            i++;
        }
        int zereNum=getZeroNum(arr1);
        int [] newNum = new int[zereNum];
        System.arraycopy(arr1,0,newNum,0,zereNum);
        return newNum;
    }
    public static int getZeroNum(int [] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==0){
                return i;
            }
        }
        return -1;
    }
    public static int getCount(int num,int [] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==num){
                count++;
            }
        }
        return count;
    }
}

3. (简答题)(Analyze scores) Write a program that reads an unspecified number of scores and  determines how many scores are above or equal to the average and how many  scores are below the average. Enter a negative number to signify the end of the  input. Assume that the maximum number of scores is 100.

import java.util.Arrays;
import java.util.Scanner;

public class Demo9 {
    public static void main(String[] args) {
import java.util.Scanner;

public class Demo10 {
    public static void main(String[] args) {
        int [] score = score();
        double average =average(score);
        int passScore = pass(score,average);
        int unPassScore = unPass(score,average);
        System.out.println("There are "+passScore+" scores are above or equal to the average");
        System.out.println("There are "+unPassScore+" scores are below the average");
    }
    public static int[] score(){

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of scores :");
        int num = sc.nextInt();
        int [] score = new int[num];
        System.out.println("Enter the scores(between 0 and 100) :");
        for (int i = 0; i < num; i++) {
            score[i]=sc.nextInt();
            if (score[i]<0){
                break;
            }
            if (score[i]>100){
                System.out.println("Invalid score!");
                System.exit(-1);
            }
        }
        return score;
    }
    public static double average(int [] arr){
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum+=arr[i];
        }
        double average = sum/arr.length;
        return average;
    }
    public static int pass(int [] arr,double average){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]>=average){
                count++;
            }
        }
        return count;
    }
    public static int unPass(int [] arr,double average){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]<average){
                count++;
            }
        }
        return count;
    }
}

 

4. (简答题)(Average an array) Write two overloaded methods that return the average of an  array with the following headers: 

public static int average(int[] array) 

public static double average(double[] array) 

Write a test program that prompts the user to enter ten double values, invokes this  method, and displays the average value.

import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) {
        System.out.println("Enter 10 number :");
        Scanner sc = new Scanner(System.in);
        double [] array = new double[10];
        for (int i = 0; i < 10; i++) {
            array[i]=sc.nextDouble();
        }
        System.out.println("The average is "+average(array));
    }
    public static int average(int [] array){
        double sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum+=array[i];
        }
        int average = (int)sum/array.length;
        return average;
    }
    public static double average(double [] array){
        double sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum+=array[i];
        }
        double average = sum/array.length;
        return average;
    }
}

5. (简答题)(Find the smallest element) Write a method that finds the smallest element in an  array of double values using the following header: public static double min(double[] array) Write a test program that prompts the user to enter ten numbers, invokes this  method to return the minimum value, and displays the minimum value. Here is a  sample run of the program:

    Enter ten numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2 

    The minimum number is: 1.5

import java.util.Scanner;

public class Demo12 {
    public static void main(String[] args) {
        System.out.print("Enter ten numbers :");
        Scanner sc = new Scanner(System.in);
        double [] arr = new double[10];
        for (int i = 0; i < 10; i++) {
            arr[i]=sc.nextDouble();
        }
        double min = min(arr);
        System.out.print("The minimum number is "+min);
    }
    public static double min(double[] arr){
        double min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(min>arr[i]){
                min=arr[i];
            }
        }
        return min;
    }
}

6. (简答题)(Computing gcd) Write a method that returns the gcd of an unspecified number  of integers. The method header is specified as follows: 

    public static int gcd(int... numbers) 

Write a test program that prompts the user to enter five numbers, invokes the  method to find the gcd of these numbers, and displays the gcd.

import java.util.Scanner;

public class Demo13 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter five numbers :");
        int [] arr = new int[5];
        for (int i = 0; i < 5; i++) {
            arr[i]=sc.nextInt();
        }
        gcd(arr);
    }
    public static int gcd(int...numbers){
        int min = min(numbers);
        boolean flag = true;
        for (int i = min; i >0; i--) {
            for (int i1 = 0; i1 < numbers.length; i1++) {
                if(numbers[i1]%i!=0){
                    flag =false;
                }
            }
            if (flag){
                System.out.println("The gcd is "+i);
                return 0;
            }
        }
        return -1;
    }
    public static int min(int[] arr){
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(min>arr[i]){
                min=arr[i];
            }
        }
        return min;
    }
}

7. (简答题)(Print distinct numbers) Write a program that reads in ten numbers and displays  the number of distinct numbers and the distinct numbers separated by exactly one  space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the  array, ignore it.) After the input, the array contains the distinct numbers. Here is  the sample run of the program:

    Enter ten numbers: 1 2 3 2 1 6 3 4 5 2 

    The number of distinct number is 6 

    The distinct numbers are: 1 2 3 6 4 5

import java.util.Scanner;

public class Demo14 {
    public static void main(String[] args) {
        System.out.print("Enter ten numbers :");
        Scanner sc = new Scanner(System.in);
        int [] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        for (int i = 0; i < arr.length; i++) {
            for(int j = i;j<arr.length;j++){
                if (arr[i]==arr[j]&&i!=j){
                    arr[j]=-1;
                }
            }
        }
        int count= 0 ;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=-1){
                count++;
            }
        }
        System.out.println("The number of distinct number is "+count);
        System.out.print("The distinct numbers are :");
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]!=-1){
                System.out.print(arr[i]+" ");
            }
        }
    }
}

8. (简答题)(Sort students) Write a program that prompts the user to enter the number of students, the students’names, and their scores, and prints student names in decreasing order of their scores.

Please submit the source code in the text form, and attach a picture to show the output of your program.

import java.util.Scanner;

public class Demo15 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of students:");
        int num = sc.nextInt();
        String [] name = new String[num];
        int [] score = new int[num];
        for (int i = 0; i < num; i++) {
            System.out.println("Enter the name of this student:");
            name[i]=sc.next();
            System.out.println("Enter the score of this student");
            score[i]=sc.nextInt();
        }
        listSort(score,name);
    }
    public static void listSort(int [] score,String [] name){
        for (int i = 0; i < score.length-1; i++) {
            for (int j = 0; j < score.length-1-i; j++) {
                if (score[j]<score[j+1]){
                    int temp = score[j];
                    score[j]=score[j+1];
                    score[j+1]=temp;
                    String temp1=name[j];
                    name[j]=name[j+1];
                    name[j+1]=temp1;
                }
            }
        }
        System.out.println("name----------------score");
        for (int i = 0; i < score.length; i++) {
            System.out.println(" "+name[i]+"                    "+score[i]);
        }
    }
}

9. (简答题)(Sort characters in a string) Write a method that returns a sorted string using the  following header: public static String sort(String s) For example, sort("acb") returns abc. Write a test program that prompts the user to enter a string and displays the sorted  string.

import java.util.Scanner;

public class Demo16 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String:");
        String str = sc.next();
        String sort = sort(str);
        System.out.println(sort);
    }
    public static String sort(String s){
        char [] ch = new char[s.length()];
        for (int i = 0; i < s.length(); i++) {
            ch[i]=s.charAt(i);
        }
        for (int i = 0; i < s.length()-1; i++) {
            for (int j = 0; j < s.length()-1-i; j++) {
                if (ch[j]>ch[j+1]){
                    char temp = ch[j];
                    ch[j]=ch[j+1];
                    ch[j+1]=temp;
                }
            }
        }
        String str = "";
        for (int i = 0; i < ch.length; i++) {
            str= str+ch[i];
        }
        return str;
    }
}

10. (简答题)(Revise selection sort) The selection-sort method repeatedly finds the smallest number in the current  array and swaps it with the first. Write a new program by finding the largest number and swapping it with the last. Write a test program that reads in ten double  numbers, invokes the method, and displays the sorted numbers.

import java.util.Arrays;
import java.util.Scanner;

public class Demo17 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter ten double numbers :");
        double [] num = new double[10];
        for (int i = 0; i < 10; i++) {
            num[i]=sc.nextDouble();
        }
        for (int i = 0; i < num.length-1; i++) {
            for (int j=i; j < num.length; j++) {
                if(num[i]<num[j]){
                    double temp = num[i];
                    num[i]=num[j];
                    num[j]=temp;
                }
            }
        }
        System.out.println(Arrays.toString(num));
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值