Home > other >  Android Studio: Random number generator repeatedly returns 0s
Android Studio: Random number generator repeatedly returns 0s

Time:01-10

I'm currently taking an online course in Java using Android Studio. As part of the course we had an exercise to build Higher or Lower number guessing game. The initial code I wrote worked just fine (despite being a bit wordy, but I'm still learning), it generated a random number and everything worked right with all the correct messaging. Here is the working code.

import java.util.Random;

public class MainActivity extends AppCompatActivity {

        Random ran = new Random();
        int gameStart = 1   (int) (Math.random() * (30));

    public void buttonClick(View view) {
        EditText number = (EditText) findViewById(R.id.number);
        Button button = (Button) findViewById(R.id.button);
        int num = Integer.parseInt(number.getText().toString());


        if (num == gameStart) {

            Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();

        } else if (num > gameStart) {

            Toast.makeText(this, "Lower", Toast.LENGTH_SHORT).show();
        } else {

            Toast.makeText(this, "Higher", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }
    }

But then the instructor has introduced extra code to make game restart if the answer is correct with a new number by making Random a method and adding it in the initial if statement. In the video it worked but when I added it and compiled the only number Random generates is 0. I have tried everything but for the love of god can't get it to work correctly. Can someone please have a look at the changes below and spot what could be creating this issue? What needs to be ammended?

public class MainActivity extends AppCompatActivity {

    int gameStart;

    public void gameRestart() {

        Random ran = new Random();
        int gameStart = 1   (int) (Math.random() * (30));

    }
    public void buttonClick(View view) {
        EditText number = (EditText) findViewById(R.id.number);
        Button button = (Button) findViewById(R.id.button);
        int num = Integer.parseInt(number.getText().toString());


        if (num == gameStart) {

            Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();
            gameRestart();

        }

CodePudding user response:

 int gameStart;

    public void gameRestart() {    
        Random ran = new Random();
        int gameStart = 1   (int) (Math.random() * (30));    
    }

you never alter the value of instance variable gameStart, so it keeps the default value, which is 0. You create a local variable gameStart in your constructor, which isn't used afterwards.

Alter your code to this to set the instance variable:

 int gameStart;

    public void gameRestart() {
        Random ran = new Random();
        gameStart = 1   (int) (Math.random() * (30));
    }

EDIT: as @AndyTurner remarked: you 're also not using your ran instance of the Random class, so you can delete that variable as well.

CodePudding user response:

public void gameRestart() {
    Random ran = new Random();
    int gameStart = 1   (int) (Math.random() * (30));
}

Here, I suppose you want to store your random value inside the member variable called gameStart. It is not the case because you redefine a local variable with the same number in the method. You just have to delete the int before your variable (and delete the ran line because unused)

public void gameRestart() {
    gameStart = 1   (int) (Math.random() * (30));
}

CodePudding user response:

you can try this;

final int random = new Random().nextInt(50)   30; 
// it will generate random number between 30 & 80
  •  Tags:  
  • Related