IDK为什么我的代码在这个循环中只接受一次输入而一直崩溃



这个问题输入了学生的名字和他们的分数,并声明了最高分,但是输出不起作用。

在这里,我正在寻找最高分,始终保持当前最高分的值,请帮助我,如果我错过了什么。

import java.util. * ;
public class Topper {
public static void main(String args[]) {
Scanner sc = new Scanner(System. in );
String studentTemp = "";
String students[] = new String[100];
int i = 0,
j = 0,
temp = 0,
n = 0;
int score[] = new int[100];
do {
System.out.println("Enter the Name");
students[i] = sc.nextLine();
System.out.println("Enter the Score");
score[i] = sc.nextInt();
n++;
i++;
} while ( students [ i ] != "Alldone");
for (i = 0; i < n; i++) {
for (j = i + 1; j < n - 1; j++) {
if (score[j] > score[i]) {
temp = score[j];
score[j] = score[i];
score[i] = temp;
studentTemp = students[i];
students[i] = students[j];
students[j] = studentTemp;
}
}
}
System.out.println("The Student with Highest score : " + students[n - 1] + " with score : " + score[n - 1]);
System.out.println("The Students performance list : ");
for (i = n - 1; i >= 0; i++)
System.out.println(students[i] + " " + score[i]);
}
}

试试这个

import java.util.*;
public class Topper
{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String studentTemp="";
String students[]=new String[100];
int i=0,j=0,temp=0,n=0,stop=0;
int score[]=new int[100];
do
{
System.out.println("Enter the Name");
students[i]=sc.next();
System.out.println("Enter the Score");
score[i]=sc.nextInt();
System.out.println("Do you wish to continue n Press 0 to continue n Press 1 to exit");
stop=sc.nextInt();
n++;
i++;
}
while(stop!=1);
temp=score[0];
studentTemp=students[0];
for(i=1;i<n;i++)
{
if(score[i]>temp)
{
temp=score[i];
studentTemp=students[i];
}
}
System.out.println("The Student with Highest score : "+ studentTemp +" with score : "+temp);
System.out.println("The Students performance list : ");
for(i=0;i<n;i++)
System.out.println(students[i]+" "+score[i]);
}
}```

最新更新