gpt4 book ai didi

java - 尝试运行其实现的内容时未使用扫描仪

转载 作者:行者123 更新时间:2023-12-01 19:55:59 25 4
gpt4 key购买 nike

所以我试图使用扫描仪来填充一个数组,我已经正确设置了它,当我运行程序时,它让我填充第一个数组,然后第二个数组完全跳过扫描仪部分。我尝试了很多方法,并得出结论,这与同时使用两台扫描仪有关。我希望输入总体记录,然后它可以让我输入部门记录,但它不允许我输入任何值。如果您尝试运行它,区分大小写并不重要,并且输入数据时应该使用空格、逗号或破折号等分隔数字的方式来完成。

import java.util.*;

public class TeamInfo {

public static String[] teamsAFC = {"Ravens", "Bills", "Bengals", "Browns", "Broncos",
"Texans", "Colts", "Jaguars", "Chiefs", "Chargers", "Dolphins", "Patriots", "Jets",
"Raiders", "Steelers", "Titans"};

public static String[] teamsNFC = {"Cardinals", "Falcons", "Panthers", "Bears", "Cowboys",
"Lions", "Packers", "Rams", "Vikings", "Saints", "Giants", "Eagles", "49ers",
"Seahawks", "Buccaneers", "Redskins"};

private static String conference, team;
public static int[] overall = new int[3];
public static int[] division = new int[3];

public static Scanner kb = new Scanner(System.in);

public TeamInfo() {

}

public static void teamPick() {
do {
System.out.print("Pick an NFL Confrence (AFC/NFC): ");
conference = kb.next();

if (conference.toUpperCase().equals("AFC")) {
for(int i = 0; i < teamsAFC.length; i++) {
System.out.println(teamsAFC[i]);
}
}
else if (conference.toUpperCase().equals("NFC")) {
for(int i = 0; i < teamsNFC.length; i++) {
System.out.println(teamsNFC[i]);
}
}
else {
System.out.println("Invalid input please try again......");
}
} while (!conference.toUpperCase().equals("AFC") && !conference.toUpperCase().equals("NFC"));

System.out.print("Pick a team from the list: ");
team = kb.next();
}

public static void overall() {
System.out.print("Enter the teams overall record(include ties): ");
while (!kb.hasNext()) {
for(int i = 0; i < overall.length; i++) {
overall[i] = kb.nextInt();
}
}
}

public static void division() {
System.out.print("Enter the teams division record(include ties): ");
while (!kb.hasNext()) {
for(int i = 0; i < division.length; i++) {
division[i] = kb.nextInt();
}
}
}

public static void finalOutput() {
System.out.println("\t" + team.toUpperCase());
System.out.println("--------------------------------");
}

public static void main(String[] args) {
teamPick();
overall();
division();
finalOutput();
}
}

最佳答案

删除 overall()division() 方法中的 while 循环应该可以解决您的问题。

您只能输入 overall() 输入的原因是,当您尝试调用 !kb.hasNext() 时,它仅对最后一个结果求值为 true在 teamPick() 方法中输入。因此,一旦您输入新值,!kb.hasNext() 就会变为 false,这就是为什么它不允许您输入除法方法的输入。

关于java - 尝试运行其实现的内容时未使用扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59042531/

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