2017-08-21 134 views
-1

對不起,如果我沒有正確提交此問題。這是我第一次來這裏。雖然循環導致應用程序凍結

所以我是編程和應用程序開發新手。過去幾周我一直在學Java,並試圖製作一個Android應用程序。這只是一個簡單的猜數字遊戲。

因此,我現在的代碼在哪裏,如果它只是幾個「如果」的話,它會工作。當然,因爲它不是一個循環,所以你只能得到一個猜測。

當我把它放在while循環中時,當while循環嘗試循環時,應用程序將凍結。

下面是代碼:

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

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

回答

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)); 
     } 
    } 
});