Practice outputting the highest and second scores

The code is as follows:

//Practice outputting the highest and second scores import Java util. Scanner; Class demo20 {public static void main (string [] args) {scanner SC = new scanner (system. In); system. Out. Println ("please enter the number of students:"); int num = sc.nextint(); string name1 = ""; / / save the information of the first high school student int Score1 = 0; string Name2 = ""; / / save the information of the second high school student int score2 = 0; / / read the name and score of each student string name = "" " ; int score=0; For (int i = 1; I < = num; I + +) {system. Out. Println ("please enter student's name:"); name = sc.next(); system. Out. Println ("please enter student's grade:"); score = sc.nextint(); if (score > Score1) {score2 = Score1; Score1 = score; Name2 = name1; name1 = name;} else if(score>score2){ score2=score; name2=name; } } If (Num > = 2) {system.out.println ("highest score: + Score1 +", name: + name1); system.out.println ("second score: + score2 +", name: + Name2);} else if(num>=1) System. out. Println ("highest score:" + Score1 + ", name:" + name1 "); else System. out. Println ("no students");}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>