Java: Rock Paper Scissors game loop

Joe Castello :

I'm trying to get this loop to work, but can't get it figured out, tried a few different kinds and haven't had any luck, gone back through some of my studying and poked around to try to get some insight but haven't been able to successfully get it to work. the base program code is as follow, basically this was a project i did a few weeks ago, and a new project wants us to go back in and have it so the game continuously plays until the user inputs a "3". I can't figure it out, I can't seem to find any examples or help online. I'm not looking for someone to just give an answer, just looking for a nudge in the right direction.

TL;DR: the game should repeat until the user inputs 3

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("scissor (0), rock (1), paper (2): ");
        int user = input.nextInt();

        int computer = (int) (Math.random() * 3);

        System.out.print("The Computer is ");
        switch (computer) {
            case 0:
                System.out.print("scissor. ");
                break;
            case 1:
                System.out.print("rock. ");
                break;
            case 2:
                System.out.print("paper. ");
        }
        System.out.print(" You are ");
        switch (user) {
            case 0:
                System.out.print("scissor");
                break;
            case 1:
                System.out.print("rock");
                break;
            case 2:
                System.out.print("paper");
        }

        if (computer == user) {
            System.out.println(" too.  It is a draw");
        } else {
            boolean win = (user == 0 && computer == 2)
                    || (user == 1 && computer == 0)
                    || (user == 2 && computer == 1);
            if (win) {
                System.out.println(". You won!");
            } else {
                System.out.println(". You lose!");
            }

        }
    }
}
Kit Ostrihon :

You can put all your code in your main method into an infinite loop and exit the program when the user inputs 3 like this.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    while(true) { //start of the loop

        Scanner input = new Scanner(System.in);

        //setting the variable to an incorrect value, 
        //so the text is printed always at least once
        int user = -1;

        //while the input is incorrect (lower than 0 or higher than 3)
        while(user < 0 || user > 3) {

            //ask for the input
            System.out.print("scissor (0), rock (1), paper (2), exit (3): ");

            //try reading an integer, as the user might input whatever (String, float,..)
            try {
                user = input.nextInt(); //trying to read an integer
            } catch (Exception e) { //in case of an invalid input (not an integer)
                //I still want to "read" the tokens, 
                //because the .nextInt() did not process the input
                input.next();
            }
            if (user == 3) System.exit(0);
        }

        //rest of your code

    } //end of the loop
}

You can see, that I used try and catch to check for other inputs than an integer. I also repeat asking for the input until it is valid. You might not necessarily need that if it is not part of your focus right now and exchange it just for the following.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    while(true) { //start of the loop, loops forever unless the user inputs 3

        Scanner input = new Scanner(System.in);

        System.out.print("scissor (0), rock (1), paper (2): ");
        int user = input.nextInt(); //trying to read an integer
        if (user == 3) System.exit(0); //if the input is 3, exit the program

        //rest of your code

    } //end of the loop
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=367025&siteId=1