Throughout
Throughout

Reputation: 13

How to receive output from a random switch statement in Android?

I'm creating a simple game of rock-paper-scissors, and I have to receive a random output from Android depending on the number variant I choose (0 = scissors, 1 = rock, 2 = paper).

It's pretty much complete, but I'm receiving no output in either TextView when I type in a number and click the 'shoot' button.

package com.example.brandon.rockpaperscissors;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText player = (EditText) findViewById(R.id.txtPlayer);
        final TextView android = (TextView) findViewById(R.id.txtAndroid);
        final TextView result = (TextView) findViewById(R.id.txtResult);
        Button shoot = (Button) findViewById(R.id.btnShoot);

        shoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int num = (int)(Math.random() *  (3) + 0);

                switch (num) {

                    //If computer picks scissors
                    case 0:

                        if (player.equals('0')) {

                            android.setText("0!");
                            result.setText("It's a Tie!");

                        } else if (player.equals('1')) {

                            android.setText("0!");
                            result.setText("You Win!");

                        } else if (player.equals('2')) {

                            android.setText("0!");
                            result.setText("Android Wins!");

                        }
                        break;

                    //If computer picks rock
                    case 1:

                        if (player.equals('0')) {

                            android.setText("1!");
                            result.setText("Android Wins!");

                        } else if (player.equals('1')) {

                            android.setText("1!");
                            result.setText("It's a Tie!");

                        } else if (player.equals('2')) {

                            android.setText("1!");
                            result.setText("You Win!");

                        }
                        break;

                    //If computer picks paper
                    case 2:

                        if (player.equals('0')) {

                            android.setText("2!");
                            result.setText("You Win!");

                        } else if (player.equals('1')) {

                            android.setText("2!");
                            result.setText("Android Wins!");

                        } else if (player.equals('2')) {

                            android.setText("2!");
                            result.setText("It's a Tie!");

                        }
                        break;

                }
            }

        });

        }
    }

Any help would be greatly appreciated, thank you!

Upvotes: 1

Views: 131

Answers (3)

AL.
AL.

Reputation: 37778

I think the reason you are not getting any output is because the way you retrieve the text from the Player EditText. The proper way to retrieve the text from an EditText is like so:

player.getText().toString();

Only then you should use the .equals like so:

player.getText().toString().equals("0")

Try it out and tell me if it works. For more info about EditTexts. Check here - EditText.

Upvotes: 1

Sweeper
Sweeper

Reputation: 271820

Your problem is that you are comparing the text that the player entered wrongly i.e. here:

if (player.equals('0')) {

As you can see, player is of type EditText. So if you compare an EditText with a character, it always evaluates to false because they are completely different things. I think you actually want to compare the text of the edit text with 0, right? Just like this:

if (player.getText().toString().equals("0")) {

You can also add in a call to trim() so that whitespace characters will be ignored:

if (player.getText().toString().trim().equals("0")) {

Also, remember to change the other wrong if statements as well.

Upvotes: 1

Archie Azares
Archie Azares

Reputation: 19

You're not printing any output. That is the reason why you don't get the output.

Upvotes: -1

Related Questions