gpt4 book ai didi

java - 填充对象数组时 readInt() 上的扫描仪错误

转载 作者:行者123 更新时间:2023-11-30 06:18:58 26 4
gpt4 key购买 nike

我正在尝试迭代对象数组,并使用扫描仪设置其中一个实例变量。这是引发错误的代码:

 public void populate(){
int participantID = 0;
Scanner pop = new Scanner(System.in);
while(participantID < participants.length){
participants[participantID] = new Participant();
System.out.println("Time in Seconds?");
int time = pop.nextInt();
participants[participantID].setTotalTime(time);
participantID++;
}
pop.close();
}

这会导致 int time = pop.nextInt(); 行出现错误。即

Exception in thread "main" java.util.NoSuchElementException
Time in Seconds? at java.util.Scanner.throwFor(Scanner.java:862)

这真的让我很困惑,因为这个方法工作得很好:

    public void create(){
Scanner scan = new Scanner(System.in);

System.out.println("What is the name of the triathlon?");
raceName = scan.nextLine();

System.out.println("How many participants were there?");
participants = new Participant[scan.nextInt()];

scan.close();
}
<小时/>

应用程序.java

    public class Application {
public static void main(String[] args){
Race mainRace = new Race();
mainRace.create();
System.out.println(mainRace.toString());
mainRace.populate();
System.out.println(mainRace.toString());
}
}

Race.java

import java.util.Scanner;

public class Race {
private String raceName;
private Participant participants[];

public Race(){

}

public String toString() {
return "Race{" +
"raceName='" + raceName + '\'' +
", participants=" + participants.length +
'}';
}

public void create(){
Scanner scan = new Scanner(System.in);

System.out.println(Colours.getAnsiYellow() + "What is the name of the triathlon?" + Colours.getAnsiReset());
raceName = scan.nextLine();

System.out.println(Colours.getAnsiYellow() + "How many participants were there?" + Colours.getAnsiReset());
participants = new Participant[scan.nextInt()];

scan.close();
}

public void populate(){
int participantID = 0;
Scanner pop = new Scanner(System.in);
while(participantID < participants.length){
participants[participantID] = new Participant();

System.out.println(Colours.getAnsiYellow() + "Time in Seconds?" + Colours.getAnsiReset());
int time = pop.nextInt();
participants[participantID].setTotalTime(time);
System.out.println(participants[participantID]);
participantID++;

}
pop.close();

}

}

参与者.java

public class Participant {
private int id;
private int runTime;
private int swimTime;
private int bikeTime;
private int totalTime;

public Participant(){
}

public int getTotalTime() {
return totalTime;
}

public void setTotalTime(int totalTime) {
this.totalTime = totalTime;
}
}

颜色.java

public class Colours {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_YELLOW = "\u001B[33m";

public static String getAnsiReset() {
return ANSI_RESET;
}


public static String getAnsiYellow() {
return ANSI_YELLOW;
}

}

最佳答案

问题不在于您的 populate 方法。问题是,您在 create 方法中创建的 Participant 数组在您关闭扫描程序后将不再存在。因此,请在 create 方法中调用 populate 方法,而不是在 Application 类中调用。

主要:

public class Application {
public static void main(String[] args) {
Race mainRace = new Race();
mainRace.create();
System.out.println(mainRace.toString());
}
}

创建方法:

public void create() {

Scanner scan = new Scanner(System.in);

System.out.println(Colours.getAnsiYellow() + "What is the name of the triathlon?" + Colours.getAnsiReset());
raceName = scan.nextLine();

System.out.println(Colours.getAnsiYellow() + "How many participants were there?" + Colours.getAnsiReset());
participants = new Participant[scan.nextInt()];

populate();

scan.close();


}

关于java - 填充对象数组时 readInt() 上的扫描仪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48573436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com