javastringif-statementrandomwhile-loop

Difficulty with Rock Paper Scissors game


I'm making a code in Java that is a rock-paper-scissors game. In particular, I am trying to make it so that the randomized outputs, aka if a user loses, wins, or ties, it is counted into the score.

For example, the user inputs either rock, paper, or scissors. If they choose rock, they will get a random output that determines whether or not they win, lose, or tie. If they got the output, let's say, "You chose rock. I chose paper. You lose!" I want to add that score result as "1 loss." in my while loop. However, I don't know how to make the program count the random outputs like that.

My code is below and I put comments in it to try and better explain what I want to do.

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

public class rockpaperscissors {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

while(true){

    System.out.println("Please choose (r)ock, (p)aper or (s)cissors:");
    String answer = input.nextLine();
    int win = 0;
    int lose = 0;
    int tie = 0;
    
    if(answer.equals("r")) {
        String[] str = { "You chose rock. I chose paper. You lose!", /* lose++; */ "You chose rock. I chose scissors. You win!",  /* win++; */ "You chose rock. I chose rock. We tie!"  /* tie++; */ };
        Random r = new Random();
        System.out.println(str[r.nextInt(3)]);
        
        
     
    }
    if(answer.equals("p")) {
        String[] str = { "You chose paper. I chose scissors. You lose!",  /* lose++; */ "You chose paper. I chose rock. You win!",  /* win++; */ "You chose paper. I chose paper. We tie!"  /* tie++; */ };
        Random r = new Random();
        System.out.println(str[r.nextInt(3)]);
   
  }
    if(answer.equals("s")) {
        String[] str = { "You chose scissors. I chose rock. You lose!",  /* lose++; */ "You chose scissors. I chose paper. You win!",  /* win++; */ "You chose scissors. I chose scissors. We tie!"  /* tie++; */ };
        Random r = new Random();
        System.out.println(str[r.nextInt(3)]);
   
  }
  
    
    System.out.println("Score: " + win + " win(s), " + lose + " loss(es), " + tie + " tie(s)");
  System.out.println("Play again (y/n)?"); 
   String c = input.nextLine();

   if(c.equalsIgnoreCase("n")){ 
      break;
     }

}

}
}

I've tried using switch statements, but they haven't been working out. I've also been trying to use if statements but my code never works. Please note that I'm still fairly new to java so there are functions that I am quite unfamiliar with.


Solution

  • You need to initialise the counter variables outside of the while loop because variables inside of the loop will be reset every time the loop executes (because "win = 0" will get executed and win will get the value of 0 at every start of the loop. What you want is that every new loop, the value of win is the value it had after the previous loop was completed).

    example code

    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        int win = 0;
        int lose = 0;
        int tie = 0;
        while(true){
    
            System.out.println("Please choose (r)ock, (p)aper or (s)cissors:");
            String answer = input.nextLine();
    
            if(answer.equals("r")) {
                String[] str = { "You chose rock. I chose paper. You lose!", /* lose++; */ "You chose rock. I chose scissors. You win!",  /* win++; */ "You chose rock. I chose rock. We tie!"  /* tie++; */ };
                Random r = new Random();
                int i = r.nextInt(3);
    
                if( i == 0 ) ++lose;
                if( i == 1 ) ++win;
                if( i == 2 ) ++tie;
                System.out.println(str[i]);
    
    
    
            }
            if(answer.equals("p")) {
                String[] str = { "You chose paper. I chose scissors. You lose!",  /* lose++; */ "You chose paper. I chose rock. You win!",  /* win++; */ "You chose paper. I chose paper. We tie!"  /* tie++; */ };
                Random r = new Random();
                int i = r.nextInt(3);
    
                if( i == 0 ) ++lose;
                if( i == 1 ) ++win;
                if( i == 2 ) ++tie;
                System.out.println(str[i]);
    
            }
            if(answer.equals("s")) {
                String[] str = { "You chose scissors. I chose rock. You lose!",  /* lose++; */ "You chose scissors. I chose paper. You win!",  /* win++; */ "You chose scissors. I chose scissors. We tie!"  /* tie++; */ };
                Random r = new Random();
                int i = r.nextInt(3);
    
                if( i == 0 ) ++lose;
                if( i == 1 ) ++win;
                if( i == 2 ) ++tie;
                System.out.println(str[i]);
    
            }
    
    
            System.out.println("Score: " + win + " win(s), " + lose + " loss(es), " + tie + " tie(s)");
            System.out.println("Play again (y/n)?");
            String c = input.nextLine();
    
            if(c.equalsIgnoreCase("n")){
                break;
            }
    
        }
    
    }