隐藏的随机数确认java中的错误结果

时间:2017-11-16 01:14:53

标签: java random increment android-studio-3.0 decrement

我对我要做的事情有一些问题,但最紧迫的是:

我有一个设置隐藏随机数字的按钮,用户必须尝试找出该数字是什么。我目前的代码如下:

//设置变量

int max = 9;
int min = 0;
int diff = max-min;
int getNum1;
int a;

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

//将Java分配给XML

final TextView urNum1 = findViewById(R.id.txtUrNum1);
final TextView myNum1 = findViewById(R.id.txtMyNum1);
ImageButton up1 = findViewById(R.id.btnUp1);
ImageButton dn1 = findViewById(R.id.btnDn1);
Button chk = findViewById(R.id.btnCheck);
ImageButton start = findViewById(R.id.btnStartGuess);

//启动随机数发生器

start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Random rn = new Random();
        urNum1.setText("?");
        a = rn.nextInt(diff+1);
        a += min;
    }
});

//检查匹配

chk.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (a == 0 && getNum1 == 9) {
            urNum1.setText("0"); }
        else if (a == 1 && getNum1 == 0) {
            urNum1.setText("1"); }
        else if (a == 2 && getNum1 == 1) {
            urNum1.setText("2"); }
        else if (a == 3 && getNum1 == 2) {
            urNum1.setText("3");  }
        else if (a == 4 && getNum1 == 3) {
            urNum1.setText("4"); }
        else if (a == 5 && getNum1 == 4) {
            urNum1.setText("5"); }
        else if (a == 6 && getNum1 == 5) {
            urNum1.setText("6"); }
        else if (a == 7 && getNum1 == 6) {
            urNum1.setText("7"); }
        else if (a == 8 && getNum1 == 7) {
            urNum1.setText("8"); }
        else if (a == 9 && getNum1 == 8) {
            urNum1.setText("9"); }

//递增猜测的数字

up1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getNum1 = Integer.parseInt(myNum1.getText().toString());
                if (getNum1 == 0) {
                    myNum1.setText("1"); }
                else if (getNum1 == 1 ) {
                    myNum1.setText("2"); }
                else if (getNum1 == 2) {
                    myNum1.setText("3"); }
                else if (getNum1 == 3) {
                    myNum1.setText("4"); }
                else if (getNum1 == 4) {
                    myNum1.setText("5"); }
                else if (getNum1 == 5) {
                    myNum1.setText("6"); }
                else if (getNum1 == 6) {
                    myNum1.setText("7"); }
                else if (getNum1 == 7) {
                    myNum1.setText("8"); }
                else if (getNum1 == 8) {
                    myNum1.setText("9"); }
                else if (getNum1 == 9) {
                    myNum1.setText("0"); }
                });

//递减猜测的数字

dn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getNum1 = Integer.parseInt(myNum1.getText().toString());
                if (getNum1 == 0) {
                    myNum1.setText("9"); }
                else if (getNum1 == 9 ) {
                    myNum1.setText("8"); }
                else if (getNum1 == 8) {
                    myNum1.setText("7"); }
                else if (getNum1 == 7) {
                    myNum1.setText("6"); }
                else if (getNum1 == 6) {
                    myNum1.setText("5"); }
                else if (getNum1 == 5) {
                    myNum1.setText("4"); }
                else if (getNum1 == 4) {
                    myNum1.setText("3"); }
                else if (getNum1 == 3) {
                    myNum1.setText("2"); }
                else if (getNum1 == 2) {
                    myNum1.setText("1"); }
                else if (getNum1 == 1) {
                    myNum1.setText("0"); }
            }
        });

我遇到的问题是当按下up1时,我检查时数字往往会匹配,但是当我按dn1来获得匹配的数字时,我选择的数字低于匹配的数字2 。这可能是因为我的编码很复杂,而且我可能会使用++递增更好,但我似乎无法让它正常工作。

1 个答案:

答案 0 :(得分:0)

如果有人遇到这个问题,我自己已经找到了解决方案。在我通过递增/递减值来更改myNum值的代码中,我只检查'if'语句的每个条件的当前值,而不更改其中的值。所以不管我检查多少次,它总是一次性的。我的修复:

getNum1 = Integer.parseInt(myNum1.getText().toString());
    if (getNum1 == 0) {
        myNum1.setText("9");
        getNum1 = 9; }
    else if (getNum1 == 9 ) {
        myNum1.setText("8");
        getNum1 = 8; }
    else if (getNum1 == 8) {
        myNum1.setText("7");
        getNum1 = 7; }

等等。