我的Android App Guessing Game中的错误

时间:2017-02-17 20:34:55

标签: java android alert alertdialog android-alertdialog

我希望有人可以帮忙解决这个问题。我有以下猜测游戏的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("");


                   }
               }
           }
    );

}

}

1 个答案:

答案 0 :(得分:1)

当你在这一行的if语句内时;

if (attempts++ > maxAttempts)

确保maxAttempts2而不是1。因为您只需要3次尝试,所以它应该是0 , 1 and 2 ..

所以你应该开始行

final int maxAttempts = 2;