我正在创建一个应用程序3X3 Tic tac toe,其中3X3是所有按钮,并且在点击监听器上我正在检查两个玩家中的任何一个是否已经制作了他们的" O"或" X"。但在我的" if-else"部分它没有检查条件,而是直接执行" if和else"部分即使条件错误。 我希望我的问题是可以理解的并且可以调试。任何帮助都会很明显......
@Override
public void onClick(View v) {
int id=v.getId();
if(id==ONEbtn.getId()){ // 1stnd button**********************************************************8
String get1=ONEbtn.getText().toString();
if(!get1.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
ONEbtn.setText(turn);
String get2=TWObtn.getText().toString();
String get3=THREEbtn.getText().toString();
String get4=FOURbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get9=NINEbtn.getText().toString();
if(get1.equals(get2) && get1.equals(get3)) { // comparing 2nd and 3rd button text with 1st btn
//Log.d("Value of 2btn", get2);
callToast(name, turn);}
else if (get1.equals(get4) && get1.equals(get7)) // comparing 4th and 7th btn with first btn
{callToast(name, turn);}
else if (get1.equals(get5) && get1.equals(get9)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ name);
}
}else if (id==TWObtn.getId()){ // 2nd button**********************************************************8
String get2=TWObtn.getText().toString();
if(!get2.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
TWObtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get3=THREEbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get8=EIGHTbtn.getText().toString();
if (get1.equals(get2) && get3.equals(get2))
{callToast(name,turn);}
else if (get5.equals(get2) && get8.equals(get2)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ name);}
}else if (id==THREEbtn.getId()){ // 3rd button**********************************************************8
String get3=THREEbtn.getText().toString();
if(!get3.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
THREEbtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get2=TWObtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get6=SIXbtn.getText().toString();
String get9=NINEbtn.getText().toString();
if (get1.equals(get3) && get2.equals(get3))
{callToast(name,turn);}
else if (get5.equals(get3) && get7.equals(get3)){
callToast(name, turn);}
else if (get6.equals(get3) && get9.equals(get3)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}else if (id==FOURbtn.getId()){ // 4th button**********************************************************8
String get4=FOURbtn.getText().toString();
if(!get4.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
FOURbtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get6=SIXbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
if (get1.equals(get4) && get7.equals(get4))
{callToast(name,turn);}
else if (get5.equals(get4) && get6.equals(get4)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}else if (id==FIVEbtn.getId()){ // 5th button**********************************************************8
String get5=FIVEbtn.getText().toString();
if(!get5.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
FIVEbtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get9=NINEbtn.getText().toString();
String get2=TWObtn.getText().toString();
String get8=EIGHTbtn.getText().toString();
String get3=THREEbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get4=FOURbtn.getText().toString();
String get6=SIXbtn.getText().toString();
if (get1.equals(get5) && get9.equals(get5))
{callToast(name,turn);}
else if (get2.equals(get5) && get8.equals(get5)){
callToast(name, turn);}
else if (get3.equals(get5) && get7.equals(get5)){
callToast(name, turn);}
else if (get4.equals(get5) && get6.equals(get5)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}else if (id==SIXbtn.getId()){ // 6th button**********************************************************8
String get6=SIXbtn.getText().toString();
if(!get6.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
SIXbtn.setText(turn);
String get3=THREEbtn.getText().toString();
String get9=NINEbtn.getText().toString();
String get4=FOURbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
if (get3.equals(get6) && get9.equals(get6))
{callToast(name,turn);}
else if (get4.equals(get6) && get5.equals(get6)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}else if (id==SEVENbtn.getId()){ // 7th button**********************************************************8
String get7=SEVENbtn.getText().toString();
if(!get7.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
SEVENbtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get4=FOURbtn.getText().toString();
String get3=THREEbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get8=EIGHTbtn.getText().toString();
String get9=NINEbtn.getText().toString();
if (get1.equals(get7) && get4.equals(get7))
{callToast(name,turn);}
else if (get5.equals(get7) && get3.equals(get7)){
callToast(name, turn);}
else if (get8.equals(get7) && get9.equals(get7)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}else if (id==EIGHTbtn.getId()){ // 8th button**********************************************************8
String get8=EIGHTbtn.getText().toString();
if(!get8.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
EIGHTbtn.setText(turn);
String get2=TWObtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get9=NINEbtn.getText().toString();
if (get2.equals(get8) && get5.equals(get8))
{callToast(name,turn);}
else if (get7.equals(get8) && get9.equals(get8)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}else if (id==NINEbtn.getId()){ // 9th button**********************************************************8
String get9=NINEbtn.getText().toString();
if(!get9.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
NINEbtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get3=THREEbtn.getText().toString();
String get6=SIXbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get8=EIGHTbtn.getText().toString();
if (get1.equals(get9) && get5.equals(get9))
{callToast(name,turn);}
else if (get3.equals(get9) && get6.equals(get9)){
callToast(name, turn);}
else if (get7.equals(get9) && get8.equals(get9)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ " "+name);}
}
}
public void callToast(String nm, String trn) {
Toast.makeText(this,nm+ " Wins with sign :"+trn,Toast.LENGTH_LONG).show();}
public String switchingTurn( String Sign ){
if (Sign.equals("0")){
name=PL_A;
playerA.setText(TAG+" "+ name);
playerB.setText("");
Toast.makeText(this, name+" turn", Toast.LENGTH_SHORT).show();
turn="X";
}else if (Sign.equals("X")){
name=PL_B;
playerB.setText(TAG+" "+ name);
playerA.setText("");
Toast.makeText(this, name+" turn", Toast.LENGTH_SHORT).show();
turn="0";
}
return turn;
}
答案 0 :(得分:0)
我做了一个非常愚蠢的错误...我所做的只是取值,然后设置值并继续比较旧的取值... 这是调试代码的一小部分......
if(id==ONEbtn.getId()){ // 1stnd button**********************************************************8
String g1=ONEbtn.getText().toString(); // corrected here!!!
if(!g1.equals("")){
Toast.makeText(this, "Not allowed here!!!", Toast.LENGTH_SHORT).show();
}
else {
ONEbtn.setText(turn);
String get1=ONEbtn.getText().toString();
String get2=TWObtn.getText().toString();
String get3=THREEbtn.getText().toString();
String get4=FOURbtn.getText().toString();
String get7=SEVENbtn.getText().toString();
String get5=FIVEbtn.getText().toString();
String get9=NINEbtn.getText().toString();
if(get1.equals(get2) && get1.equals(get3)) { // comparing 2nd and 3rd button text with 1st btn
// Log.d("Value of 2btn","<<"+get1+"<<"+get3+"<<"+ get2+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
callToast(name, turn);}
else if (get1.equals(get4) && get1.equals(get7)) { // comparing 4th and 7th btn with first btn
callToast(name, turn);}
else if (get1.equals(get5) && get1.equals(get9)){
callToast(name, turn);}
turn=switchingTurn(turn);
Log.d(TAG, turn+ name);
}
}