按钮单击时生成新的随机数

时间:2017-06-04 14:26:26

标签: android button random

我有一个数字猜测应用程序,当用户找到随机数时,会出现一个新按钮,这是一个再次播放按钮。当用户点击它时,它应该生成一个新的随机数,以便用户可以找到它,但它没有,它会生成相同的旧随机数

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;



import java.util.Random;

public class MainActivity extends AppCompatActivity {
Button button;
Button btnReplay;
int randomNumber;

public void checkGuess(View view){

    button = (Button)findViewById(R.id.button);

    EditText editText = (EditText)findViewById(R.id.editText);
    ImageView imgCheck = (ImageView)findViewById(R.id.imageViewCheck);
    ImageView imgWrong = (ImageView)findViewById(R.id.imageViewWrong);
    String edittextString = editText.getText().toString()
    Integer edittextInt = Integer.parseInt(edittextString);
    MediaPlayer mediaPlayer = MediaPlayer.create(this, 
          R.raw.correctanswer);
    btnReplay.setVisibility(View.INVISIBLE);
    if (edittextInt > randomNumber){
        imgWrong.setVisibility(View.VISIBLE);
        imgCheck.setVisibility(View.INVISIBLE);
        button.setVisibility(View.VISIBLE);
        btnReplay.setVisibility(View.INVISIBLE);
        Toast.makeText(getApplicationContext(), "لەم ژمارەیە نزمترە!" , 
           Toast.LENGTH_LONG).show();
               } else if (edittextInt < randomNumber){
         imgWrong.setVisibility(View.VISIBLE);
        imgCheck.setVisibility(View.INVISIBLE);
        Toast.makeText(getApplicationContext(), "لەم ژمارەیە زیاترە", 
               Toast.LENGTH_LONG).show();
        button.setVisibility(View.VISIBLE);
        btnReplay.setVisibility(View.INVISIBLE);
    }
    else if (edittextInt == randomNumber){
        Toast.makeText(getApplicationContext(), "زۆر باشە ! ژمارەکەت 
               زانی", Toast.LENGTH_LONG).show();
        imgWrong.setVisibility(View.INVISIBLE);
        imgCheck.setVisibility(View.VISIBLE);
        mediaPlayer.start();
        btnReplay.setVisibility(View.VISIBLE);
        button.setVisibility(View.INVISIBLE);
    }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);       
    Random random = new Random();    
    randomNumber= random.nextInt(51);
    new AlertDialog.Builder(this).setTitle("بیخوێنەوە").setMessage("ژمارە 
      ٠٥٠").setNeutralButton("OK", null).show(); 
    btnReplay = (Button)findViewById(R.id.btnReplay);
    btnReplay.setOnClickListener(new View.OnClickListener() {
        @Override
                public void onClick(View v) {
                    Math.random();
                    checkGuess(findViewById(R.id.btnReplay));
                 }
            });       
        }   
     }

1 个答案:

答案 0 :(得分:0)

使用

生成随机数
randomNumber= random.nextInt(51);

我认为此行位于onCreate()您的活动类中。 (您应该编辑您的问题以澄清这一点。)换句话说,您只在创建应用程序时生成一个随机数。如果您希望每次用户单击按钮时生成随机数,则必须使用正确的方法onClick()执行此操作。