Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class HighScores {
- /*
- * Method: main()
- * Purpose: plays the game as long as users wants shouldPlayAgain() return true.
- */
- public static void main(String args[])
- {
- do{
- playGame();
- }while (playGameAgain());
- }
- /*
- * Method: playGameAgain()
- * Purpose: Ask the player if they want to go again.
- * Input: None..
- * Returns: boolean (play again or not?)
- */
- private static boolean playGameAgain()
- {
- char playAgain = 'y';
- Scanner input = new Scanner(System.in);
- while(playAgain != 'n')
- {
- System.out.println("Go again?(y/n)");
- /// PROBLEM HERE.... Somehow, it will NOT wait for INPUT. It just throws an error
- playAgain = input.next().charAt(0);
- }
- input.close();
- return (playAgain != 'y')?false:true;
- }
- /*
- * Method: playGame()
- * Purpose: Play one game.
- * Input: None..
- * Returns: void
- */
- private static void playGame()
- {
- ArrayList<String> names = new ArrayList<String>();
- ArrayList<Integer> scores = new ArrayList<Integer>();
- initialize(names, scores);
- sort(names, scores);
- display(names, scores);
- }
- /*
- * Method: initialize(ArrayList<String> names, ArrayList<Integer> scores)
- * Purpose: User inputs 5 sets of players & their scores.
- * Input: names arraylist & interger arraylist
- * Returns: void.
- */
- public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
- {
- Scanner input = new Scanner(System.in);
- for (int i = 1; i <= 5; i++)
- {
- System.out.println("Enter the name for score #" + i + ":");
- names.add(input.nextLine());
- System.out.println();
- System.out.println("Enter the score for score #" + i + ":");
- while(!input.hasNextInt())
- {
- //input.nextLine();
- System.out.println("Please input a valid integer value for the score for #" + i + ":");
- if(!input.hasNextInt())
- input.nextLine();
- }
- scores.add(input.nextInt());
- input.nextLine();
- }
- input.close();
- }
- /*
- * Method: sort(ArrayList<String> names, ArrayList<Integer> scores)
- * Purpose: sorts the scores (and corresponding players) in descending order.
- * Input: names arraylist & interger arraylist
- * Returns: void.
- */
- public static void sort(ArrayList<String> names, ArrayList<Integer> scores)
- {
- // bubble sort, i.e. Selection Sort Algorithm
- //int index, maxIndex;
- for(int i= 0; i < (scores.size()-1); i++)
- {
- int maxValue = scores.get(i);
- for (int j = i+ 1; j < scores.size(); j++)
- {
- if(scores.get(j) > maxValue)
- {
- // get the max for the scores & put it on top..
- int temp = scores.get(i);
- maxValue = scores.get(j);
- scores.remove(i);
- scores.add(i, maxValue);
- scores.remove(j);
- scores.add(j, temp);
- //get the corresponding data for the usernames.
- String temp1 = names.get(i);
- String maxName = names.get(j);
- names.remove(i);
- names.add(i, maxName);
- names.remove(j);
- names.add(j, temp1);
- }
- }
- }
- }
- /*
- * Method: display(ArrayList<String> names, ArrayList<Integer> scores)
- * Purpose: displays the scores (and corresponding players) in descending order.
- * Input: names arraylist & interger arraylist
- * Returns: void.
- */
- public static void display(ArrayList<String> names, ArrayList<Integer> scores)
- {
- System.out.println("Top Scorers:");
- for (int i = 0; i < names.size(); i++ )
- {
- System.out.println(names.get(i) + ":" + scores.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement