2015-11-01 37 views
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; 



} 

      } 

回答

0

我編寫了一些代碼,將其作爲我的解決方案來解決您的問題。 我會創建一個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]); 
    } 
} 

}

很明顯,我還沒有花時間來正確地創建對象,但希望你能看到的想法,儘管糟糕的代碼。希望這可以幫助!

+0

我必須使用void inputScores(int [],String)方法爲每個遊戲輸入分數。一個數組來保存玩家的分數和玩家的名字 –

+0

editted,希望這可以幫助 –

+0

我明白你的意思,但它有點令人困惑,因爲如果你瞭解這種新的類型,你使用不同的代碼。 –