简体   繁体   中英

Cannot change the boolean value to true

When I execute the code, it always goes to the else condition. But I want the if statement to run when score=true; I cannot figure out how to do this...kindly help me out. If not this way, is there any other way I can approach?

public class withComp extends Activity {

 public boolean isPlayer2=false, score=false;
 @Override
     public void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);    
      setContentView(R.layout.withcomp);
      final Button one = (Button)findViewById(R.id.one); 
      final TextView winner = (TextView)findViewById(R.id.winner);

        one.setOnClickListener(new View.OnClickListener() 
        {
           @Override
           public void onClick(View v) 
               {
               if(isPlayer2==false)
                  {
                one.setText("X");
                score = true;
                one.setEnabled(false);
                isPlayer2 = true;
              }
               else
               {
                  one.setText("O");
                  one.setEnabled(false);
                  isPlayer2=false;
               }
          }
   });
     if(score == true)
     {
       winner.setText("won");
     }
    else {
      winner.setText("lose");
         }
  }
}

Just use

if(score)

Without the comma

You don't need to write if (variable == true) or if (variable == false)

It's much better just to write if (variable) or if (!variable)

  • You're executing the If-Else only once when the activity is created. Move the if-else-code into the OnClickListener.
  • And of course remove the semicolon after the if.

For example:

public class withComp extends Activity {

  public boolean isPlayer2=false, score=false;
  @Override
     public void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.withcomp);

      final Button one = (Button)findViewById(R.id.one); 
      final TextView winner = (TextView)findViewById(R.id.winner);

      one.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
             if (!isPlayer2) {
                one.setText("X");
                score = true;
                one.setEnabled(false);
                isPlayer2 = true;
              } else {
                  one.setText("O");
                  one.setEnabled(false);
                  isPlayer2=false;
              }

              // XXX Move it here
              if (score) {
                winner.setText("won");
              } else {
                winner.setText("lose");
              }
          }
       });
   }
}

Your problem lies in if(score == true);

if clauses do not need a semicolon after the condition, but a statement. Giving it a semicolon passes it the null statement, meaning nothing will happen.

After that it will execute both the blocks following, overwriting what happens in the block you intended to be executed if score is true .

You can fix this simply by removing the semicolon, letting it correctly execute the block.

Semicolon means end of statement.Remove ;

from if(score == true);

Use it this way"

if(score){
 winner.setText("won");
}
else {
 winner.setText("lose");
}

a few things,

  • one as already was pointed out, you are using semicolon ; after if statement, and i'm supprised you were able to compiled and execute this code
  • two, your if-else block is executed in method create and you setting value of score in listener, which means, block if-else will be executed after you create your activity, while value score will be setted to true after press button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM