Facade
Facade

Reputation: 7

I am trying to add an option to replay or end the game. Also, my results aren't popping up properly

I am trying to add an exit option to this code. Also, my results will not read correctly, and I am not sure what I am doing wrong. I've tried using a do while loop, but I can't seem to place it in the correct place.

//import Scanner Class
import java.util.Scanner;
import java.util.Random;

public class JavaMidterm {
    public static void main(String[] args) {
        String result = " ", symbola = " ", symbolb = " ";
        Scanner s = new Scanner(System.in);
        //prompt user
        System.out.print("Scissor (0), rock (1), paper (2): ");
        // read user choice
        int choice=s.nextInt();
        //Create random class object
        Random random = new Random();
        //Generate a random number from 0, 1, 2
        int pick = random.nextInt(3);
        if(choice == pick)
            result = "It is a draw";
        else {
            if (((choice == 0) && (pick == 2)) || ((choice == 1) && (pick ==0)) || ((choice == 2) && (pick == 1)) )
                result = "You won";
            else
                result = "You lose";
        }

        if(pick == 0)
            symbola = "Scissor";
        if(choice == 0)
            symbolb = "Scissor";
            //assigning symbols to the corresponding values
        if(pick == 1)
            symbolb = "Rock";
        if(pick == 2)
            symbola = "Paper";
        if(choice == 2)
            symbolb = "Paper";
        System.out.println("The computer is" +
            symbola + ". You are" + symbolb + ". " + result);
    }
}

Upvotes: 0

Views: 38

Answers (2)

Mark Melgo
Mark Melgo

Reputation: 1478

You can try my solution below. I have also included checking for invalid inputs. I also simplified the checking for your result.


//import Scanner Class
import java.util.Random;
import java.util.Scanner;

public class JavaMidterm {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        // Create random class object
        Random random = new Random();
        String[] choices = new String[] { "Scissor", "Rock", "Paper" };

        int choice;

        // prompt user
        System.out.print("Scissor (0), rock (1), paper (2): ");
        while (true) {
            try {
                String result = "";
                // read user choice
                choice = Integer.parseInt(s.nextLine());
                if (choice < 0 || choice > 2) {
                    throw new Exception();
                }
                // Generate a random number from 0, 1, 2
                int pick = random.nextInt(3);

                if (choice == pick)
                    result = "It is a draw";
                else if (((choice == 0) && (pick == 2)) || ((choice == 1) && (pick == 0))
                        || ((choice == 2) && (pick == 1)))
                    result = "You won";
                else
                    result = "You lose";

                System.out.println("The computer is " + choices[pick] + ". You are " + choices[choice] + ". " + result);

                System.out.print("Do you want to play again? Yes (0), No (1): ");
                while (true) {
                    try {
                        // read user choice
                        choice = Integer.parseInt(s.nextLine());
                        if (choice < 0 || choice > 1) {
                            throw new Exception();
                        }
                        if (choice == 0)
                            System.out.print("Scissor (0), rock (1), paper (2): ");
                        break;
                    } catch (Exception e) {
                        System.out.println("Invalid input.");
                        System.out.print("Do you want to play again? Yes (0), No (1): ");
                    }
                }

                if (choice == 1) {
                    break;
                }

            } catch (Exception e) {
                System.out.println("Invalid input.");
                System.out.print("Scissor (0), rock (1), paper (2): ");
            }
        }
        s.close();
    }
}

Upvotes: 1

resatz
resatz

Reputation: 576

I assume that you wish to end the game in case of a "win" and continue the game in case of "losing". Assuming that the code can be modified like below,

boolean isGoodToContinue = true;
    while (isGoodToContinue) {
        String result = " ", symbola = " ", symbolb = " ";
        Scanner s = new Scanner(System.in);
        // prompt user
        System.out.print("Scissor (0), rock (1), paper (2): ");
        // read user choice
        int choice = s.nextInt();
        // Create random class object
        Random random = new Random();
        // Generate a random number from 0, 1, 2
        int pick = random.nextInt(3);
        if (choice == pick)
            result = "It is a draw";
        else {
            if (((choice == 0) && (pick == 2)) || ((choice == 1) && (pick == 0))
                    || ((choice == 2) && (pick == 1))) {
                result = "You won";
                isGoodToContinue = false;
            }
            else {
                result = "You lose";
                isGoodToContinue = true;
            }
        }

Upvotes: 1

Related Questions