dimanche 5 février 2017

Android Studio: Can I use randomized numbers for a switch statement?

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;

            }
            }




Aucun commentaire:

Enregistrer un commentaire