我有這個程序,要求用戶輸入學生和他們的得分。然後顯示得分最高和最低的學生的名字。我的問題是我的程序不會打印最低分數。Java數組diplay最低學生
任何幫助將不勝感激。 下面是我的代碼。
private Scanner n;
private int limit;
private String[] names;
private int[] scores;
public Exercise9() {
n = new Scanner(System.in);
limit = 0;
index();
}
public void index() {
int counter = 0;
System.out.print("Enter how many students : ");
limit = Integer.parseInt(n.nextLine());
names = new String[limit];
scores = new int[limit];
while(counter < limit && limit != 0) {
System.out.print("Enter name : ");
names[counter] = n.nextLine();
System.out.print("Enter score : ");
scores[counter] = Integer.parseInt(n.nextLine());
counter += 1;
}
if(limit == 0) {
System.out.print("\n\nNo student(s) to display .");
} else {
displayStudents();
}
}
public void displayStudents() {
System.out.print("\n\nDisplay list of student(s) and scores : \n");
for(int x = 0 ; x < limit ; x++) {
System.out.println("\t" + names[x] + "\t" + scores[x]);
}
System.out.print("\n\nHighest : " + getHighest() + "\t\tLowest : " + getLowest());
}
public String getHighest() {
int theScore = 0;
String theName = "";
for(int x = 0 ; x < scores.length; x++) {
if(scores[x] > theScore) {
theScore = scores[x];
theName = names[x];
}
}
return theName;
}
public String getLowest() {
int theScore = 0;
String theName = "";
for(int x = 0 ; x < scores.length; x++) {
if(scores[x] < theScore) {
theScore = scores[x];
theName = names[x];
}
}
return theName;
}
public static void main(String[] args) {
new Exercise9();
}