gpt4 book ai didi

java - 我如何获得数组来保存所玩游戏的数量并为每个玩家运行它?

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:03 25 4
gpt4 key购买 nike

我有一个作业,其中两个用户要输入他们的名字,然后获取他们玩的游戏数量,然后获取他们玩的每场游戏的分数,将分数放入一个数组(或两个数组???)中,然后比较两个数组和分数以查看谁赢得了每场比赛或他们是否平局,然后显示结果。我不相信我不需要对它进行排序或搜索。

我的老师说获取分数必须是一个 void 方法,但我不确定如何从这个 void 方法中将值获取到每个玩家的数组中,以便我可以比较分数值。

    public class Lab9 {
public static void inputScores(int[] array, String name) {
String inputScores = "";
for (int i = 1; i < array.length; i++) {
inputScores = JOptionPane.showInputDialog(name + " enter your score for game " + i);
array[i] = Integer.parseInt(inputScores);
}
}

public static void main(String[] args) {

int numberOfGames = getPositiveIntOrQuit("How many games were played?", "Lab 9 (by Jarvis),");



String name = getStringOrQuit("Player 1-What is your name?", "Lab 9 (by Jarvis");

String name1 = getStringOrQuit(" Player 2 - What is your name?","Lab 9(by Jarvis");


int score = getNonNegativeIntOrQuit(name + " enter your score for game" , "Lab 9 (by Jarvis)");



}

public static String getStringOrQuit(String prompt, String title) {
String userInputString;

userInputString = JOptionPane.showInputDialog(null, prompt, title, JOptionPane.QUESTION_MESSAGE);
// Did user hit Cancel or OK with nothing typed?
if (userInputString == null || userInputString.trim().equals("")) {
JOptionPane.showMessageDialog(null,"No input, so program will terminate now.");
System.exit(0);
}

return userInputString;
} // getStringOrQuit



public static int getPositiveIntOrQuit(String prompt, String title) {
String userInputString;
int userInputInt = 0;

do {
userInputString = JOptionPane.showInputDialog(null, prompt, title, JOptionPane.QUESTION_MESSAGE);
// Did user hit Cancel or OK with nothing typed?
if (userInputString == null || userInputString.trim().equals("")) {
JOptionPane.showMessageDialog(null,"No input, so program will terminate now.");
System.exit(0);
}
else
try {
userInputInt = Integer.parseInt(userInputString); // This line might throw an exception.
// Ok, if conversion in above line worked, check if input is a positive integer.
if (userInputInt < 1)
JOptionPane.showMessageDialog(null,"Bad input value. It must be a positive integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}
catch (NumberFormatException exc) {
JOptionPane.showMessageDialog(null,"Bad input value. It must be a positive integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}

} while (userInputInt < 1);

return userInputInt;
} // getPositiveIntOrQuit



public static int getNonNegativeIntOrQuit(String prompt, String title) {
String userInputString;
int userInputInt = -1;

do {
userInputString = JOptionPane.showInputDialog(null, prompt, title, JOptionPane.QUESTION_MESSAGE);
// Did user hit Cancel or OK with nothing typed?
if (userInputString == null || userInputString.trim().equals("")) {
JOptionPane.showMessageDialog(null,"No input, so program will terminate now.");
System.exit(0);
}
try {
userInputInt = Integer.parseInt(userInputString); // This line might throw an exception.
// Ok, if conversion in above line worked, check if input is a positive integer.
if (userInputInt < 0)
JOptionPane.showMessageDialog(null,"Bad input value. It must be a non-negative integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}
catch (NumberFormatException exc) {
JOptionPane.showMessageDialog(null,"Bad input value. It must be a non-negative integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}

} while (userInputInt < 0);

return userInputInt;



}

}

最佳答案

我已经写了一些代码,将其作为我解决您问题的方法。我将创建一个 HashMap (或两个,每个玩家一个)并将游戏编号映射到分数。然后,您可以使用此 HashMap 轻松比较分数

示例(这是我很快就完成的粗略代码):

public class random {

private static Scanner keyboard = new Scanner(System.in);
private static HashMap<Integer, String> gameMap = new HashMap<Integer, String>();

public static void mapScores(){
System.out.println("How many games have been played?");
int numOfGames = keyboard.nextInt();

String score = "";

for(int i = 0; i < numOfGames; i++){
System.out.println("Please enter the score for game: " + i);
score = keyboard.nextLine();
gameMap.put(i, score);
}


}

}

编辑:

好吧,那么我建议静态声明你的整数数组。方便访问。你说的是一个保存玩家分数和玩家姓名的数组,这就是 id 建议使用 HashMap 的原因,因为数组无法存储名称。我将演示:

public class random {

private static Scanner keyboard = new Scanner(System.in);
private static HashMap<String, int[]> gameMap = new HashMap<String, int[]>();
private static int[] hello = new int[10];

public static void mapScores(int[] array, String name){
String inputScores = "";
for (int i = 1; i < array.length; i++) {
inputScores = JOptionPane.showInputDialog(name + " enter your score for game " + i);
array[i] = Integer.parseInt(inputScores);
}
}

public static void main(String[] args) {

random.gameMap.put("name", new int[10]);
random.mapScores(gameMap.get("name"), "name");

for(int i = 0; i < gameMap.get("name").length; i++){
System.out.println(gameMap.get("name")[i]);
}
}

}

显然我没有花时间正确创建对象,但希望你能看到这个想法,尽管代码很糟糕。希望这有帮助!

关于java - 我如何获得数组来保存所玩游戏的数量并为每个玩家运行它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33458098/

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