用户获得正确的号码后如何更改随机数?

时间:2016-11-26 05:01:32

标签: java android-studio

如何在用户输入正确的号码后更改随机数?我试过了,但似乎如果我使用while,数字总是正确的,所以它会产生无限循环

    final TextView randomnumber = (TextView) findViewById(R.id.randomTextView);
    final EditText number = (EditText) findViewById(R.id.editText2);
    final EditText number2 = (EditText) findViewById(R.id.editText);
    final EditText yourchoose = (EditText) findViewById(R.id.choose);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int a = Integer.parseInt(number.getText().toString());
            int b = Integer.parseInt(number2.getText().toString());
            int d = (int) (Math.random() * (a - b) + b);
            int c = Integer.parseInt(yourchoose.getText().toString());
            if(d > c) {
                randomnumber.setText("Choose Bigger");
            } else if (d < c) {
                randomnumber.setText("Choose Smaller");
            } else {
                randomnumber.setText("You're Right, The number is " + d);
            }
        }
    });

2 个答案:

答案 0 :(得分:1)

onClick之外生成一个随机数。从while中删除onClick循环。 请参阅下面的代码,

final TextView randomnumber = (TextView) findViewById(R.id.randomTextView);
final EditText number = (EditText) findViewById(R.id.editText2);
final EditText number2 = (EditText) findViewById(R.id.editText);
final EditText yourchoose = (EditText) findViewById(R.id.choose);

Random rand = new Random();
final int randomNumber = rand.nextInt();

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int a = Integer.parseInt(number.getText().toString());
        int b = Integer.parseInt(number2.getText().toString());
        int c = Integer.parseInt(yourchoose.getText().toString());

        int random = (int) (randomNumber * (a - b) + b);
        if (random > c) {
            randomnumber.setText("Choose Bigger");
        } else if (random < c) {
            randomnumber.setText("Choose Smaller");
        } else {
            randomnumber.setText("Your're Right, Number is " + random);
        }

    }
});

修改

再次看一下,生成的随机数应该在以下范围之间

  

b&lt; randomNumber&lt; (a - b)

所以我使用TextWatcher收听numbernumber2 EditText上的chnage。

// Before onCreate
int a = 0, b = 0, c, random = 0;

//inside onCreate
final TextView randomnumber = (TextView) findViewById(R.id.randomTextView);
final EditText number = (EditText) findViewById(R.id.editText2);
final EditText number2 = (EditText) findViewById(R.id.editText);
final EditText yourchoose = (EditText) findViewById(R.id.choose);

final Random rand = new Random();
number.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        a = Integer.parseInt(number.getText().toString());
        random = rand.nextInt(a - b) + b;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after){}
});

number2.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        b = Integer.parseInt(number2.getText().toString());
        random = rand.nextInt(a - b) + b;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after){}
});

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int c = Integer.parseInt(yourchoose.getText().toString());

        if (random > c) {
            randomnumber.setText("Choose Bigger");
        } else if (random < c) {
            randomnumber.setText("Choose Smaller");
        } else {
            randomnumber.setText("Your're Right, Number is " + random);
        }

    }
});

答案 1 :(得分:0)

因为c和random的值在while循环中没有变化,所以它继续。

例如:如果random的值为5且c为3.一旦进入循环,值保持不变,而(random!= c)始终为true,因此它不会停止。