简体   繁体   English

Android Studio:随机数生成器反复返回0s

[英]Android Studio: Random number generator repeatedly returns 0s

I'm currently taking an online course in Java using Android Studio.我目前正在使用 Android Studio 参加 Java 的在线课程。 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.但是,然后讲师引入了额外的代码,如果答案是正确的,则通过使 Random 成为方法并将其添加到初始 if 语句中,使用新数字重新启动游戏。 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.在视频中它起作用了,但是当我添加它并编译时,随机生成的唯一数字是 0。我已经尝试了一切,但看在上帝的份上,它无法正常工作。 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();

        }
 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.你永远不会改变实例变量 gameStart 的值,所以它保持默认值,即 0。你在构造函数中创建了一个局部变量 gameStart,之后不再使用。

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.编辑:正如@AndyTurner 所说:您也没有使用随机 class 的运行实例,因此您也可以删除该变量。

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 .在这里,我假设您想将随机值存储在名为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)您只需要删除变量之前的int (并删除ran的行,因为未使用)

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

you can try this;你可以试试这个;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM