while循环导致应用程序冻结

时间:2017-08-21 04:21:27

标签: java android

很抱歉,如果我没有正确提交此问题。这是我第一次来这里。

所以我是编码和应用程​​序开发的新手。我过去几周一直在学习Java,我正在尝试制作一个Android应用程序。它只是一个简单的猜数游戏。

所以我现在有了代码,如果它只是几个"如果"声明,它会起作用。当然,你只能得到一个猜测,因为它不是一个循环。

当我把它放在while循环中时,当while循环尝试循环时,app会冻结。

以下是代码:

package com.example.jeremy.numberguessinggame;

import org.w3c.dom.Text;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    final TextView title = (TextView) findViewById(R.id.textView1);
    final EditText userGuess = (EditText) findViewById(R.id.editText1);
    Button btnSubmit = (Button) findViewById(R.id.button1);
    final TextView guessFeedback = (TextView) findViewById(R.id.textView2);
    final TextView textNumberOfGuesses = (TextView) findViewById(R.id.textView3);
    final TextView textGuessNumber = (TextView) findViewById(R.id.textView4);
    final TextView textCorrectNumber = (TextView) findViewById(R.id.tempText);
    Random rand = new Random();
    final int correctNumber = rand.nextInt(5)+1;

    //This line is to convert the int correctNumber to a String
    textCorrectNumber.setText(String.valueOf(correctNumber));

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int numberOfTries = 0;

                //To compare (EditText)userGuess to (integer)rand, we need to get a String from userGuess, and then
                //convert that string into an integer
                String convertedString = userGuess.getText().toString();
                int convertedNumber = Integer.parseInt(convertedString);

                while (true) {
                    if (convertedNumber == correctNumber) {
                        guessFeedback.setText("You guessed the correct number");
                        numberOfTries++;
                        textGuessNumber.setText(String.valueOf(numberOfTries));
                        title.setText("YOU WON!!!!!");
                        break;
                    } else if (convertedNumber > 5 || convertedNumber < 1) {
                        guessFeedback.setText("The number is between 1 and 5.");
                        numberOfTries++;
                        textGuessNumber.setText(String.valueOf(numberOfTries));
                    } else if (convertedString.equals(null)) {
                        guessFeedback.setText("Nope.");
                    } else if (convertedNumber > correctNumber) {
                        guessFeedback.setText("Your guess is too high.");
                        numberOfTries++;
                        textGuessNumber.setText(String.valueOf(numberOfTries));
                    } else if (convertedNumber < correctNumber) {
                        guessFeedback.setText("Your guess is too low.");
                        numberOfTries++;
                        textGuessNumber.setText(String.valueOf(numberOfTries));
                    }
                }
            }

        });
}

我已经尝试用谷歌搜索了相当数量,但我没有找到太多帮助解决这个问题。以下是代码的链接:https://pastebin.com/B11pgvzD

任何人都知道它可能是什么?

1 个答案:

答案 0 :(得分:2)

你甚至不需要while循环。如果我理解正确,那么您当前的while循环位于用户输入的某种提交按钮的监听器内。好吧,每次点击按钮只需要检查一次,因为在用户提交输入之前,从业务逻辑的角度来看,它确实还没有发生。

int numberOfTries = 0;

btnSubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (convertedNumber == correctNumber) {
            guessFeedback.setText("You guessed the correct number");
            numberOfTries++;
            textGuessNumber.setText(String.valueOf(numberOfTries));
            title.setText("YOU WON!!!!!");
        } else if (convertedNumber > 5 || convertedNumber < 1) {
            guessFeedback.setText("The number is between 1 and 5.");
            numberOfTries++;
            textGuessNumber.setText(String.valueOf(numberOfTries));
        } else if (convertedString.equals(null)) {
            guessFeedback.setText("Nope.");
        } else if (convertedNumber > correctNumber) {
            guessFeedback.setText("Your guess is too high.");
            numberOfTries++;
            textGuessNumber.setText(String.valueOf(numberOfTries));
        } else if (convertedNumber < correctNumber) {
            guessFeedback.setText("Your guess is too low.");
            numberOfTries++;
            textGuessNumber.setText(String.valueOf(numberOfTries));
        }
    }
});