0
私は誰かがこれを手伝ってくれることを願っています。私はアンドロイドのために以下のゲームアプリを推測しています。それは実際にこれを行うん私のAndroid App推測ゲームのバグ
行うことになっているもの
ユーザーはそれは彼らが間違って3回を推測してきたとの答えがあると言うalertdialogがあり、間違った三回を推測したとき、ある....しかし、問題のバグは、ユーザーが3回目の試行で正しい回答を得た場合でも、正しいと判断されたアラートに対して、3回間違っているというアラートが表示されることです。両方のアラートは、この1回以外は正常に動作します。
アプリの他のすべては、私が望むように動作します。
おかげ
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("");
}
}
}
);
}
}