Skip to content

Exercise_05_34 Answer! #3

@SquirrelCoder

Description

@SquirrelCoder

Hey man,

I could not find your answer to Question No. 34 Chapter 5.
This is my answer, though I am not sure, if it's efficient or not. (Still learning :D)
If you like, you may add it to the answers :).

    public static void main(String[] args) {
        // Scissor (0)
        // Rock (1)
        // Paper (2)
        Scanner input = new Scanner(System.in);

        int computerItem;
        int numOfComputerWon = 0;
        int numOfHumanWon = 0;

        while (true) {
            System.out.print("Number of Your Points: " + numOfHumanWon);
            System.out.print("\nNumber of Computer Points: " + numOfComputerWon);
            System.out.println();
            System.out.println();
            System.out.print("The Computer is: ");
            computerItem = (int) (Math.random() * 3);
            switch (computerItem) {
                case 0:
                    System.out.print("Scissor");
                    break;
                case 1:
                    System.out.print("Rock");
                    break;
                case 2:
                    System.out.print("Paper");
                    break;
            }
            System.out.println();
            System.out.print("Scissor (0), Rock (1), Paper (2): ");
            int humanItem = input.nextInt();
            System.out.print("You are ");
            switch (humanItem) {
                case 0:
                    System.out.print("Scissor");
                    break;
                case 1:
                    System.out.print("Rock");
                    break;
                case 2:
                    System.out.print("Paper");
                    break;
            }

            System.out.println();

            if (humanItem == 0 && computerItem == 2) {
                numOfHumanWon++;
                System.out.print("You Won! 1 Point for You!");
            } else if (humanItem == 2 && computerItem == 0) {
                numOfComputerWon++;
                System.out.print("You Lost! 1 Point for Computer!");
            } else if (humanItem == 1 && computerItem == 0) {
                numOfHumanWon++;
                System.out.print("You Won! 1 Point for You!");
            } else if (humanItem == 0 && computerItem == 1) {
                numOfComputerWon++;
                System.out.print("You Lost. 1 Point for Computer!");
            } else if (humanItem == 2 && computerItem == 1) {
                numOfComputerWon++;
                numOfHumanWon++;
                System.out.print("You Won! 1 Point for You!");
            } else if (humanItem == 1 && computerItem == 2) {
                numOfComputerWon++;
                System.out.print("You Lost! 1 Point for Computer!");
            } else {
                System.out.print("Draw! Again.");
            }
            System.out.println();
            if (numOfComputerWon - numOfHumanWon >= 2) {
                System.out.print("Computer has " + (numOfComputerWon - numOfHumanWon) + " Points more than you.");
                System.out.print("\nYou Lost. Maybe Next Time!");
                break;
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions