1
我有兩個用戶輸入他們的名字,然後得到他們玩的遊戲數量,然後得到他們玩的每場比賽的比分,將得分放在一個數組(或兩個數組???)中,然後比較兩個陣列和分數,看看誰贏了每場比賽或者他們是否平局,然後顯示結果。我不需要對它進行分類或搜索,我不相信。我的老師說得到的分數必須是一個無效的方法,但我不知道我是如何從這個無效方法的值到每個球員的數組,因此我可以比較得分值。我如何獲得數組來保存所玩遊戲的數量併爲每個玩家運行?
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;
}
}
我必須使用void inputScores(int [],String)方法爲每個遊戲輸入分數。一個數組來保存玩家的分數和玩家的名字 –
editted,希望這可以幫助 –
我明白你的意思,但它有點令人困惑,因爲如果你瞭解這種新的類型,你使用不同的代碼。 –