2017-02-17 60 views
0

我希望有人可以幫助解決這個問題。我有Android的下面的猜謎遊戲應用程序。我的Android應用猜錯遊戲中的錯誤

什麼它應該做的是,當用戶猜錯了三次,它有說,他們已經猜到錯三次,答案是alertdialog ....

它真正做到這一點沒問題,但問題是,如果用戶在第三次嘗試時得到了答案,它仍然會提示警告,說他們錯了三次,而不是警告,告訴他們他們做對了。除了這一次,這兩種警報都能正常工作。

應用程序中的其他所有內容都按我的需要工作。

任何想法的人?

感謝

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

當你在該行的if語句的內部;

if (attempts++ > maxAttempts) 

確保您maxAttempts2而不是1。因爲只是你應該嘗試它3次,它應該是0 , 1 and 2 ..

所以,你應該讓你的行

final int maxAttempts = 2;