(指定等級)撰寫一個程式,讀入學生成績,獲取最髙分best, 然后根據下面的規則陚等級值
? 如果分數>=best-10, 等級為A
? 如果分數>=best-20, 等級為B
? 如果分數>=best-30, 等級為C
? 如果分數>=卜68140, 等級為D
? 其他情況下,等級為F
程式提示用戶輸入學生總數,然后提示用戶輸入所有的分數,最后顯示等級得出結論,下面
是一個運行示例:
(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 >= best -5
Grade is B if score is >= best -10;
Grade is C if score is >= best -15;
Grade is D if score is >= best -20;
Grade is F otherwise.
The program prompts the user to enter the total number of students, and 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
下面是參考答案代碼:
// https://cn.fankuiba.com
import java.util.Arrays;
import java.util.Scanner;
public class Ans7_1_page235 {
public static void main(String[] args) {
String[] grade = {"A","B","C","D","F"};
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int number = input.nextInt();
System.out.print("Enter " + number + " scores: ");
int[] score = new int[number];
for (int i = 0; i < number; i++)
score[i] = input.nextInt();
int[] scoreSort = new int[number];
System.arraycopy(score,0,scoreSort,0,score.length);
Arrays.sort(scoreSort);
int maxSort = scoreSort[number-1];
for (int i = 0; i < number; i++) {
if (score[i] >= maxSort-10)
System.out.println("Student " + i + " score is " + score[i] +
" and grade is " + grade[0]);
else if (score[i] >= maxSort-20)
System.out.println("Student " + i + " score is " + score[i] +
" and grade is " + grade[1]);
else if (score[i] >= maxSort-30)
System.out.println("Student " + i + " score is " + score[i] +
" and grade is " + grade[2]);
else if (score[i] >= maxSort-40)
System.out.println("Student " + i + " score is " + score[i] +
" and grade is " + grade[3]);
else
System.out.println("Student " + i + " score is " + score[i] +
" and grade is " + grade[4]);
}
}
}
適用Java語言程式設計與資料結構(基礎篇)(原書第11版)Java語言程式設計(基礎篇)(原書第10/11版)
發布在博客:(https://cn.fankuiba.com)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/164036.html
標籤:Java
