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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.