Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android Studio: Random number generator repeatedly returns 0s

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();

        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading