從鍵盤讀入學生成績,找出最高分, 並輸出學生成績等級
一、題目
從鍵盤讀入學生成績,找出最高分,並輸出學生成績等級。
成績>=最高分-10 等級為’A’
成績>=最高分-20 等級為’B’
成績>=最高分-30 等級為’C’
其余等級為’D’
二、代碼
import java.util.Scanner; public class arrayDemo { public static void main(String[] args) { System.out.println("請輸入學生個數:"); Scanner StuNum=new Scanner(System.in);//得到一個學生的人數number int b=StuNum.nextInt();//轉換成為int類型的number int[] score=new int[b];//將這個number作為數組的長度 for (int i=0;i<score.length;i++){ System.out.println("請輸入第"+(i+1)+"個學生成績:"); Scanner StuSco=new Scanner(System.in); int c=StuSco.nextInt(); score[i]=c; }//for循環將分數一個一個的賦給數組 System.out.println("這是學生的成績:"); for (int x:score){ System.out.print(x+" "); }//遍歷一遍看有沒有問題 int temp=0; for (int i=1;i<score.length;i++){ for (int j=0;j<score.length-1;j++){ if (score[j]>score[j+1]){ temp=score[j]; score[j]=score[j+1]; score[j+1]=temp; } } }//這里是將數組排序,數組中最后一個元素就是最大的number System.out.println("\n這是排好序的學生成績"); for (int bs:score){ System.out.print(bs+" "); }//將排序好的數組打印一遍,看有沒有問題 int m=score[b-1];//m是最高分 System.out.println("\n成績最高的分數是:"+m); char p; for (int s=0;s<score.length;s++){ int r=m-score[s];//r是最高分和他的差值 if (r<=10){ p='A'; }else if (r<=20){ p='B'; }else if (r<=30){ p='C'; }else { p='D'; } System.out.println("學生"+(s+1)+"的成績是"+score[s]+",他的評級是“"+p); } } }
三、運行結果
四、總結
這道題主要點在於如何找到MaxNumbr和如何給分數做評級,MaxNumber其實可以用代碼中雙重for(冒泡排序)中的內層for直接找到,題目並沒有說明必須按照原有順序輸出,所以對原數組進行排序,排序號后最后一個元素就是MaxNumber。對分數進行評級則采用if-else的多重判斷。