CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Loop problem

  1. #1
    Join Date
    Oct 2009
    Posts
    6

    Loop problem

    Hey im alittle stuck with this quick rock paper scissors game what i have to make the loop so that it will replay the game over and over until the user wants to quit then it will print the number of games won loss and teid. can someone help me out. here is the code



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

    public class Rpc
    {

    public static void main(String[] args)

    {

    int yourchoice;
    int compchoice;

    //Get the choice from the player
    Scanner scan = new Scanner (System.in);

    System.out.println ("Rock, Paper, or Scissors? ");
    System.out.println ("1 for Rock, 2 for Paper, 3 for Scissors ");
    yourchoice = scan.nextInt();

    //Gets the Computers choice
    Random randomnum = new Random ();

    compchoice = randomnum.nextInt(3);

    //All the if statements
    if (yourchoice == 1 && compchoice == 0)
    System.out.println( "Tie!");

    else if (yourchoice == 1 && compchoice == 1)
    System.out.println("Paper beats rock. You lose!");

    else if (yourchoice == 1 && compchoice == 2)
    System.out.println("Rock beats scissors. You win!");

    else if (yourchoice == 2 && compchoice == 0)
    System.out.println("Paper beats rock. You win!");

    else if (yourchoice == 2 && compchoice == 1)
    System.out.println("Tie!");

    else if (yourchoice == 2 && compchoice == 2)
    System.out.println("Scissors beats paper. You lose!");

    else if (yourchoice == 3 && compchoice == 0)
    System.out.println("Rock beats Scissors. You lose!");

    else if (yourchoice == 3 && compchoice == 1)
    System.out.println("Scissors beats paper. You win!");

    else if (yourchoice == 3 && compchoice == 2)
    System.out.println("Tie!");


    }


    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Loop problem

    the pseudocode for a loop like that could be something like:
    Code:
    choice = user input
    while choice != 'quit'
       process choice
       choice = user input
    end while
    I cannot teach anybody anything, I can only make them think...
    Socrates
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured