Skip to main content

I wrote a "Rock, Paper, Scissor, Shoot” game in one method using Java. I need help looping the program



I am able to loop the program, but each time I input a value it will return 2 values, the user winning and the user losing. I've experimented using multiple methods and creating a new class which was the tester, but had some problems figuring out the logic. As for loops, I have tried using a for loop, while, and do while.





Thanks in advance!










// Rock Paper Scissor Shoot Game



import java.util.Random;

import java.util.Scanner;



public class RockPaperSciccor {

public static void main(String[] args){



int wins = 0;

int losses = 0;



int rnd;





for(rnd=0;rnd<=10;rnd++)

{



Random GAME = new Random();

int PC = 1+GAME.nextInt(3);



Scanner input = new Scanner (System.in);

int SCISSOR, ROCK, PAPER;

SCISSOR = 1;

ROCK = 2;

PAPER = 3;



System.out.println("");

System.out.println("Choose Your Weapon! ");

System.out.println("1 = Scissor| 2 = Rock| 3 = Paper");

System.out.println("");

int USER = input.nextInt();



while (USER > 3) {

System.err.println("Incorrect value entered, fool");

System.err.println("Choose a number 1-3");

return;

}

System.out.println("___________________");



if(USER == PC){

if(USER == SCISSOR){

System.out.println("You Both Played Scissor");

}

if(USER == ROCK){

System.out.println("You Both Played Rock");

}

if(USER == PAPER){

System.out.println("You Both Played Paper");

}

System.out.println("Draw");

System.out.println("___________________");

System.out.println("Wins: " + wins + "| Losses: " + losses);

}

//User wins

if(USER == SCISSOR && PC == PAPER){

System.out.println("You: Scissor");

System.out.println("PC: Paper");

System.out.println("Scissor Cuts Paper");

System.out.println("You Win!");

System.out.println("___________________");

wins++;

System.out.println("Wins: " + wins + "| Losses: " + losses);

}

//Pc wins

else if(PC == ROCK){

System.out.println("You: Scissor");

System.out.println("PC: Rock");

System.out.println("Rock Breaks Scissor!");

System.out.println("PC Wins!");

System.out.println("___________________");

losses++;

System.out.println("Wins: " + wins + "| Losses: " + losses);

}

//User wins

if(USER == ROCK && PC == SCISSOR ){

System.out.println("You: Rock");

System.out.println("PC: Scissor");

System.out.println("Rock Breaks Scissor");

System.out.println("You Win! ");

System.out.println("___________________");

wins++;

System.out.println("Wins: " + wins + "| Losses: " + losses);

}

//Pc wins

else if (PC == PAPER){

System.out.println("You: Rock");

System.out.println("PC: Paper");

System.out.println("Paper Covers Rock!");

System.out.println("PC Wins!");

System.out.println("___________________");

losses++;

System.out.println("Wins: " + wins + "| Losses: " + losses);

}

//User Wins

if(USER == PAPER && PC == ROCK){

System.out.println("You: Paper");

System.out.println("PC: Rock");

System.out.println("Paper Covers Rock");

System.out.println("You Win!");

System.out.println("___________________");

wins++;

System.out.println("Wins: " + wins + "| Losses: " + losses);

}

// Pc Wins

else if (PC == SCISSOR){

System.out.println("You: Paper");

System.out.println("PC: Scissor");

System.out.println("Scissor Cuts Paper!");

System.out.println("PC Wins!");

System.out.println("___________________");

losses++;

System.out.println("Wins: " + wins + "| Losses: " + losses);

}





}











}

}




Comments

  1. Your issue is your else/if selection blocks. Each time, you allow it to continue, even if the selection went through. You also aren't checking properly. In each if/else pair you use, you check

    if(user X && pc Y) ...
    else if (pc Z) ...


    This will be evaluate one of the two blocks if the user plays X and the PC Y, OR if the PC plays z. You should have it set up to be:

    if(user X)
    if(pc Y) ...
    else if (pc Z) ...


    Or

    if(user X && pc Y) ...
    else if(user X && pc Z) ...


    I suggest the former. It is marginally more efficient because it only checks user X once. This won't make half of a noticeable difference.

    You should do it like this:

    if(user plays paper)
    if(pc plays rock)
    ...
    else if (pc plays scissors)
    ...
    else if(user plays rock)
    if(pc plays scissors)
    ...
    else if (pc plays paper)
    ...
    else if(user plays scissors)
    if(pc plays rock)
    ...
    else if (pc plays paper)
    ...

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?