user3344771
user3344771

Reputation: 1

How to conditionally loop a statement using if in Java?

I just started learning Java in a class. The assignment was to make a Rock, Paper and Scissors game, here is my source code. I turned this in already but have not received a grade.

package rockpaperscissors;

import java.util.Random;
import java.util.Scanner;

public class Main {

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

    // Introduce user to game
    System.out.println("Welcome to the Rock, Paper and Scissors game");

    // Ask the user for their move
    System.out.println("\nPlease select a move\nEnter 0 for Scissors, 1 "
            + "for Rock and 2 for Paper");
    int userMove = input.nextInt();

    // Ensure that userMove is between 0 and 2
    if (userMove > 2 || userMove < 0) {
        System.out.println("Invalid entry the result of this game will not "
                + "be accurate! Please retry using either 0 for Scissors, 1"
                + " for Rock and 2 for Paper\n");
    }

    // Generate computerMove using java.util.Random
        // Create instance first
    Random MyRandom = new Random();

    //Now generate number
    int computerMove = MyRandom.nextInt(3);

    System.out.print("The computer played " + computerMove + "\nRemember"
            + " 0 stands for Scissors, 1 is for Rock and 2 is for paper\n");

    // Determine draw result
    if (userMove == computerMove) {
        System.out.println("\nYou played " + userMove + ", The computer "
                + "played " + computerMove + " The game is a draw!");
    }
    // Determine computer win
    else if(userMove == 0 && computerMove == 1) {
        System.out.println("\nYou played " + userMove + ", The computer "
                + "played " + computerMove + " The Computer Wins!");


    }

    else if(userMove == 2 && computerMove == 0) {
        System.out.println("\nYou played " + userMove + ", The computer "
                + "played " + computerMove + " The Computer Wins!");

    }

    else if(userMove == 1 && computerMove == 2) {
        System.out.println("\nYou played " + userMove + ", The computer "
                + "played " + computerMove + " The Computer Wins!");

    }

    /* Determine User win, as no other moves can result in computer win all
    other combinations will result in user win
     */

            else {
        System.out.print("\nYou played " + userMove + ", The computer "
                + "played " + computerMove + " You Win!");
    }



}

}

I am having trouble on getting the program to re-ask for a number if the number is above 2 or below 0, it will say "Invalid entry the result will not be valid..." but it will still preform the rest of the program with an invalid number.

How can I make the program stop and prompt the user for a valid number and then preform the program? Additionally, this is the third program I have ever made so any other advice would be appreciated.

Thanks

Upvotes: 0

Views: 93

Answers (1)

Taryn East
Taryn East

Reputation: 27747

What you want is to not do all the other code if your conditions are met.

Right now you are giving an error message when your conditions are met - but not stopping execution of the rest of the function.

you can just stop.

So - without telling you the exact answer... how do you exit from a function? Do that just after the error message gets displayed and you will be fine.

However - if you want to continue fetching the user input until you meet the conditions... that requires a loop around the part that fetches and checks.

what loops do you know?

do you know one that lets you loop until you've met some condition?

you've got your condition already... now you just need to adapt a loop to let you do the code that you want - repeatedly - until that condition is met.

in pseudo code (ie this is not real java - you must translate):

some_loop do
   fetch user input
   give error message if its not ok
until (user input is ok)

Upvotes: 1

Related Questions