-1

So I'm trying to create a rock/paper/scissor app but I'm running into a problem. In the code below "Random r" chooses a number 1-3 and assigns it to R/P/S. That part works, but the scoring or toast code within my switch statement does not. The integer next to "case" should be the value of the choice correct? If anyone can find my problem I would appreciate it.

public void decide(){
        int playerTotal = 0;
        int computerTotal = 0;

        TextView pTotal = (TextView)findViewById(R.id.playerTotal);
        TextView cTotal = (TextView)findViewById(R.id.compTotal);

        pTotal.setText(Integer.toString(playerTotal));
        cTotal.setText(Integer.toString(computerTotal));

        int cpu = r.nextInt(3); //Randomizer will choose 1-3
        if(cpu == 1) {
            cpuChoice = "rock";
            imgCpu.setImageResource(R.drawable.rock);
        }
        else if(cpu == 2){
            cpuChoice = "paper";
            imgCpu.setImageResource(R.drawable.paper);
        }
        else if(cpu == 3){
            cpuChoice = "scissors";
            imgCpu.setImageResource(R.drawable.scissors);
        }
        String winOrLose = "";

            switch (cpu){
                case 1:
                    if(myChoice == "rock") {
                        winOrLose = "Stalemate!";
                    }
                    if(myChoice == "paper") {
                        winOrLose ="paper beats rock! You won this round!";
                        playerTotal++;
                    }
                    if(myChoice == "scissors") {
                        winOrLose = "rock beats paper! You lost this round!";
                        computerTotal++;
                    }
                    Toast.makeText(MainActivity.this,winOrLose, Toast.LENGTH_SHORT);
                    break;
                case 2:
                    if(myChoice == "rock") {
                        winOrLose = "paper beats rock! You lost this round!";
                        computerTotal++;
                    }
                    if(myChoice == "paper") {
                        winOrLose = "Stalemate!";
                    }
                    if(myChoice == "scissors") {
                        winOrLose = "scissors beats paper! You won this round!";
                        playerTotal++;
                    }
                    Toast.makeText(MainActivity.this,winOrLose, Toast.LENGTH_SHORT);
                    break;
                case 3:
                    if(myChoice == "rock") {
                        winOrLose = "rock beats scissors! You won this round!";
                        playerTotal++;
                    }
                    if(myChoice == "paper") {
                        winOrLose = "scissors beats paper! You lost this round!";
                        computerTotal++;
                    }
                    if(myChoice == "scissors") {
                        winOrLose = "Stalemate!";
                    }
                    Toast.makeText(MainActivity.this,winOrLose, Toast.LENGTH_SHORT);
                    break;

            }
            }
user207421
  • 305,947
  • 44
  • 307
  • 483
  • I dont see any problem with this code, could you be more specific what problem you are getting? – jack jay Feb 06 '17 at 07:48
  • [`Random.nextInt(int)`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-): "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)". => That means, you get the values 0, 1, or 2 ... and not 1, 2, or 3! – Seelenvirtuose Feb 06 '17 at 08:05
  • Yes, and Android Studio has exactly nothing to do with it. – user207421 Feb 06 '17 at 08:53

2 Answers2

0

As @Seelenvirtuose said you nextInt() will generate value between 0-2 but not 1-3 (assuming your sending 3 as the parameter for nextInt() method).

So to generate value exactly between 1-3 you can try below code.

int minimum = 1;
int maximum = 4;

Random random = new Random();

int randomValue = random.nextInt( maximum - minimum ) + minimum;

To know more about generating random number between specified range you visit here

Community
  • 1
  • 1
Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
0

I figured it out! The scoring did not work because every time the method was called, the scored would reset to 0, so I swapped it to the end of the method and erased the zeroes. For the toast, I had to add .show() at the end like so:

Toast.makeText(MainActivity.this,winOrLose, Toast.LENGTH_SHORT).show();

Thanks for the suggestions everyone! Random worked 1-3 for me, but I changed it to 0-2 to be technically correct.