Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.InputMismatchException; //error handling
- import java.util.Random; //Import this to generate pseudo-random code.
- public class Question2 {
- public static void main(String[] args) {
- String[] cardNumbers = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", //array of numbers
- "Queen", "King" };
- String[] suits = { "Spades", "Clubs", "Hearts", "Diamonds" }; //array of suits
- String decisionInput;
- Scanner keyboard = new Scanner(System.in);
- boolean repeatYes = false;
- Random r = new Random(); //Declare the random element r here.
- do {
- String[][] deck = new String[4][4]; //2D array created
- String[] randomSuits = new String[4]; //Two sets of 1D arrays to store randomized sets of
- String[] randomNumbers = new String[4]; //4 suits and numbers as per the example output.
- for (int i = 0; i < 4; i++)
- randomSuits[i] = suits[r.nextInt(4)]; //Randomized suits being assigned
- for (int i = 0; i < 4; i++)
- randomNumbers[i] = cardNumbers[r.nextInt(13)]; //Randomized card numbers being assigned
- for (int i = 0; i < 4; i++) { //Loop through 2D array, and place number + of + suits
- for (int j = 0; j < 4; j++) {
- deck[i][j] = randomNumbers[j] + " of " + suits[i];
- }
- }
- display(deck); //call display method on deck
- boolean c1Good = true, c2Good = true, validInput = false;
- int chosenColumn1 = 0;
- System.out.println("\nPlease pick a card and enter the column number (1-4) where it appears");
- while (c1Good == true)
- {
- do
- {
- try //try/catch to catch the exception thrown by the console if
- //the user inputs anything other than an integer
- {
- chosenColumn1 = keyboard.nextInt(); //ask for first column
- validInput = true; //no need to repeat
- if ((chosenColumn1 >= 1) & (chosenColumn1 <= 4)) //ensure int is between 1 and 4
- c1Good = false; //need to repeat outer loop
- else
- System.out.println("Please enter an integer value between 1 and 4");
- }
- catch (InputMismatchException e) //catches lack of integer, avoiding program error
- {
- keyboard.nextLine(); //ask for input again
- System.out.println("Please enter an integer value between 1 and 4");
- }
- }
- while (!validInput); //will keep repeating this till it gets valid input
- }
- String[][] transposedMatrix = transposeMatrix(deck); //transposes the 2D array
- display(transposedMatrix); //displays newly transposed array
- System.out.println("\nPlease Indicate which column number (1-4) it is in now");
- int chosenColumn2 = 0;
- validInput = false; //back to false for new loop use
- while (c2Good == true) //does the same user validation as the while loop before transpose
- {
- do
- {
- try
- {
- chosenColumn2 = keyboard.nextInt();
- validInput = true;
- if ((chosenColumn2 >= 1) & (chosenColumn2 <= 4))
- c2Good = false;
- else
- System.out.println("Please enter an integer value between 1 and 4");
- }
- catch (InputMismatchException e)
- {
- keyboard.nextLine();
- System.out.println("Please enter an integer value between 1 and 4");
- }
- } while (!validInput);
- }
- keyboard.nextLine(); //clears the line for new use below
- System.out.println("Your card is: " + transposedMatrix[chosenColumn1 - 1][chosenColumn2 - 1]);
- System.out.println("Do you want to try one more time? y or n");
- decisionInput = keyboard.nextLine(); //want a new iteration
- if (decisionInput.equalsIgnoreCase("n")) {
- System.out.println("Thank you for using the JAVA Magic 101 Program");
- repeatYes = false; //ends outer do-while loop and terminates program when evaluated in 'while'
- //condition below
- } else if (decisionInput.equalsIgnoreCase("y")) {
- repeatYes = true; //repeats program from the top when evaluated in 'while' condition below
- } else {
- System.out.println("\nInvalid input!"); //not required, but just in case
- }
- } while (repeatYes == true);
- }
- public static String[][] transposeMatrix(String matrix[][]) { //method for transposing the 2D
- int m = matrix.length;
- int n = matrix[0].length;
- String[][] transposedMatrix = new String[n][m]; //create the space for a transposed matrix
- for (int x = 0; x < n; x++) { //fill it with values based on original 2D
- for (int y = 0; y < m; y++) {
- transposedMatrix[x][y] = matrix[y][x];
- }
- }
- return transposedMatrix;
- }
- public static void display(String x[][]) { //method to display any 2D array
- for (int row = 0; row < x.length; row++) {
- for (int column = 0; column < x[row].length; column++) {
- System.out.printf("%-25s", x[row][column]); //printf to format where the elements are placed when printed (25 character spaces)
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement