簡體   English   中英

我的Android應用猜游戲中的錯誤

[英]Bug in my Android App Guessing Game

我希望有人可以提供幫助。 我有以下針對Android的猜謎游戲應用程序。

它應該做的是,當用戶猜錯三遍時,它會顯示一個警告對話框,提示他們三遍猜錯了,答案是...。

它確實確實可以很好地解決問題,但是存在的問題是,如果用戶在第三次嘗試中得到正確的答案,它仍然會發出警告,指出他們錯了三次,而不是警告他們說過錯了。 。 除了這一次,兩個警報都可以正常工作。

應用程序上的其他所有內容均按我的意願運行。

有任何想法嗎?

謝謝

public class Task1Activity extends AppCompatActivity {

int attempts = 0;
final int maxAttempts = 1;
Random randGen = new Random();
int ranNum;

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

    final TextView textResponse = (TextView) findViewById(R.id.txtResponse);
    final TextView guessText = (TextView) findViewById(R.id.txtAnswer);
    final EditText userGuess = (EditText) findViewById(R.id.etNumber);
    Button pressMe = (Button) findViewById(R.id.btnGuess);

    randGen = new Random();
    // Generate number once
    ranNum = randGen.nextInt(20);

    final Toast toast = Toast.makeText(getApplicationContext(), "Please guess between 0 and 20", Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 350);


    // When the button is clicked, it shows the text assigned to the txtResponse TextView box
    pressMe.setOnClickListener(new View.OnClickListener() {


   @Override
   public void onClick(View v) {
       final AlertDialog.Builder alert = new AlertDialog.Builder(Task1Activity.this);
       alert.setTitle("Unlucky");
       alert.setCancelable(false);
       alert.setMessage("You have guessed incorrectly three times. " +
               "The answer was " + ranNum + ". " + "Would you like to play again?")
               //.setCancelable(true)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       //dialog.dismiss();
                       Intent i = new Intent(Task1Activity.this, Task1Activity.class);
                       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(i);

                   }
               });

                alert
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int i) {
                        //Task1Activity.this.finish();
                       dialog.dismiss();
                       finishAffinity();
                   };
               });

       final AlertDialog.Builder alert2 = new AlertDialog.Builder(Task1Activity.this);
       alert2.setTitle("You Did It!");
       alert2.setCancelable(false);
       alert2.setMessage("The answer was " + ranNum + ". " + "Would you like to play again?")
               //.setCancelable(true)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       //dialog.dismiss();
                       Intent i = new Intent(Task1Activity.this, Task1Activity.class);
                       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(i);

                   }
               });

       alert2
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int i) {
                       //Task1Activity.this.finish();
                       dialog.dismiss();
                       finishAffinity();
                   };
               });



                       int userNumber = Integer.parseInt(userGuess.getText().toString());

                       if (userNumber > 19) {
                           //guessText.setText("Please guess between 0 and 20");
                           //guessText.setBackgroundColor(Color.WHITE);
                           toast.show();
                       } else if (userNumber < ranNum) {
                           guessText.setText("Your answer is too low. Guess again!");
                           guessText.setBackgroundColor(Color.YELLOW);
                       } else if (userNumber > ranNum) {
                           guessText.setText("Your answer is too high.  Guess again!");
                           guessText.setBackgroundColor(Color.RED);
                       } else if (userNumber == ranNum) {
                           ranNum = randGen.nextInt(20);
                           //guessText.setText("You did it!");
                           //guessText.setBackgroundColor(Color.WHITE);
                           alert2.show();
                       }

       if (attempts++ > maxAttempts) {
           alert.show();
           //guessText.setText("You have guessed incorrectly three times.  The answer was " + ranNum);
       } else {
           String randText = "";


                       randText = Integer.toString(ranNum);
                       textResponse.setText("");

                       userGuess.setText("");


                   }
               }
           }
    );

}

}

當您在此行的if語句中時;

if (attempts++ > maxAttempts)

確保您的maxAttempts2而不是1 因為您只需嘗試3次,所以它應該是0 , 1 and 2 ..

所以你應該做你的線

final int maxAttempts = 2;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM