Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. public class HighScores {
  4.  
  5. /*
  6. * Method: main()
  7. * Purpose: plays the game as long as users wants shouldPlayAgain() return true.
  8. */
  9. public static void main(String args[])
  10. {
  11. do{
  12. playGame();
  13. }while (playGameAgain());
  14. }
  15.  
  16. /*
  17. * Method: playGameAgain()
  18. * Purpose: Ask the player if they want to go again.
  19. * Input: None..
  20. * Returns: boolean (play again or not?)
  21. */
  22. private static boolean playGameAgain()
  23. {
  24. char playAgain = 'y';
  25. Scanner input = new Scanner(System.in);
  26. while(playAgain != 'n')
  27. {
  28. System.out.println("Go again?(y/n)");
  29. /// PROBLEM HERE.... Somehow, it will NOT wait for INPUT. It just throws an error
  30. playAgain = input.next().charAt(0);
  31. }
  32. input.close();
  33. return (playAgain != 'y')?false:true;
  34. }
  35.  
  36. /*
  37. * Method: playGame()
  38. * Purpose: Play one game.
  39. * Input: None..
  40. * Returns: void
  41. */
  42. private static void playGame()
  43. {
  44. ArrayList<String> names = new ArrayList<String>();
  45. ArrayList<Integer> scores = new ArrayList<Integer>();
  46.  
  47. initialize(names, scores);
  48. sort(names, scores);
  49. display(names, scores);
  50.  
  51. }
  52.  
  53. /*
  54. * Method: initialize(ArrayList<String> names, ArrayList<Integer> scores)
  55. * Purpose: User inputs 5 sets of players & their scores.
  56. * Input: names arraylist & interger arraylist
  57. * Returns: void.
  58. */
  59. public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
  60. {
  61. Scanner input = new Scanner(System.in);
  62. for (int i = 1; i <= 5; i++)
  63. {
  64. System.out.println("Enter the name for score #" + i + ":");
  65. names.add(input.nextLine());
  66. System.out.println();
  67. System.out.println("Enter the score for score #" + i + ":");
  68. while(!input.hasNextInt())
  69. {
  70. //input.nextLine();
  71. System.out.println("Please input a valid integer value for the score for #" + i + ":");
  72. if(!input.hasNextInt())
  73. input.nextLine();
  74. }
  75. scores.add(input.nextInt());
  76. input.nextLine();
  77. }
  78. input.close();
  79. }
  80.  
  81. /*
  82. * Method: sort(ArrayList<String> names, ArrayList<Integer> scores)
  83. * Purpose: sorts the scores (and corresponding players) in descending order.
  84. * Input: names arraylist & interger arraylist
  85. * Returns: void.
  86. */
  87. public static void sort(ArrayList<String> names, ArrayList<Integer> scores)
  88. {
  89. // bubble sort, i.e. Selection Sort Algorithm
  90. //int index, maxIndex;
  91. for(int i= 0; i < (scores.size()-1); i++)
  92. {
  93. int maxValue = scores.get(i);
  94. for (int j = i+ 1; j < scores.size(); j++)
  95. {
  96. if(scores.get(j) > maxValue)
  97. {
  98. // get the max for the scores & put it on top..
  99. int temp = scores.get(i);
  100. maxValue = scores.get(j);
  101. scores.remove(i);
  102. scores.add(i, maxValue);
  103. scores.remove(j);
  104. scores.add(j, temp);
  105.  
  106. //get the corresponding data for the usernames.
  107. String temp1 = names.get(i);
  108. String maxName = names.get(j);
  109. names.remove(i);
  110. names.add(i, maxName);
  111. names.remove(j);
  112. names.add(j, temp1);
  113. }
  114. }
  115. }
  116. }
  117.  
  118. /*
  119. * Method: display(ArrayList<String> names, ArrayList<Integer> scores)
  120. * Purpose: displays the scores (and corresponding players) in descending order.
  121. * Input: names arraylist & interger arraylist
  122. * Returns: void.
  123. */
  124. public static void display(ArrayList<String> names, ArrayList<Integer> scores)
  125. {
  126. System.out.println("Top Scorers:");
  127. for (int i = 0; i < names.size(); i++ )
  128. {
  129. System.out.println(names.get(i) + ":" + scores.get(i));
  130.  
  131. }
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement