2016-01-09 137 views
-1

我正在尝试清理我的代码,以使其在'部分'下运行。 有一个叫intro(),另外一个叫second()等..代码行被忽略

问题是当我代码intro())第一部分切换下一个一个(second())的应该先运行清洁代码,但不是真的!

code基本上在list应该看起来像一个聊天写入文本,所以intro()是文本的第一部分,那么就应该清除聊天并开始编写内部second()其他文本。

这是我的代码,我们来看一看:

int counter; 
boolean introDone = false; 



//Intro 
public void intro(){ 
     write(answers[counter], buttonText[counter]); 
} 

public void second(){ 
      write("So these are the rules:", R.string.go); 
} 

public void hello(View view){ 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    if(counter <= 2){ 
    intro(); 
    }else if(counter == 3){ 
     clear(); 
    }else if (counter > 2 && counter < 5) { 
    second();} 

    counter++; 
} 

}

这奏效了:

//Intro 
public void intro(){ 
     write(answers[counter], buttonText[counter]); 

} 

public void second(){ 
      write("So these are the rules:", R.string.go); 
} 

public void hello(View view) { 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    if (counter <= 2) { 
     intro(); 
    } else if(counter == 3) { 
     introDone = true; 
    } 
    if (introDone) { 
     clear(); 
     introDone = false; 
    } 
    if (counter > 2 && counter < 5) { 
      second(); 
     } 

    counter++; 
} 

}

+0

尝试if(introDone == true)..如果这样不起作用,那么ti意味着您的IntroDone为false。 – Kristo

+3

@ Kristo1990如何将if(introDone)改为if(introDone == true)会改善什么?这两个代码将以相同的方式工作,并且您的提议仅引入了像if(introDone = true)那样的错字错误的可能性,这就是为什么应该避免使用'== true'的原因。 – Pshemo

+1

这似乎并没有改变@ Kristo1990 – FET

回答

0

我要去假设布尔型introDone变量永远不会是真的。在你的intro()方法中,你用if语句包装它来检查计数器是否等于或小于2。这是很难说没有完整的源代码,但我猜想,如果你改变了你的代码,以这样的:

//Intro 
public void intro(){ 

    //if (counter <= 2) { 
     // write(answers[counter], buttonText[counter]); 
    //}else{ 
     introDone = true; 
    //} 

} 

public void second(){ 
    if(introDone) { 
     listViewMother.setAdapter(null); //THIS SHOULD CLEAN THE LIST, BUT INSTEAD WHEN I RUN THE CODE IT LOOKS LIKE IT JUST IGNORES IT 
     if (counter > 2 && counter < 5) { 
      write("So these are the rules:", R.string.go); 
     } 
    } 
} 

public void hello(View view){ 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    intro(); 
    second(); 
    counter++; 
} 

一切都将正常工作。

从那里你可以看看你如何增加计数器变量的逻辑,看看是否是这个问题。

此示例将运行call intro()和second()10次。

public void hello(View view){ 
     Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
     toast.show(); 
     for (int counter = 0; counter < 10; counter++) { 
      intro(); 
      second(); 
     } 

} 
+0

嗨,詹姆斯,我认为你是对的,你澄清了我的if/else语句的概念,所以谢谢。顺便说一句,我仍然不能真正说出这是否工作,因为我需要一些文本之前应该清理,但现在我正在找出实现t的方法他最后的结果:) – FET

+0

好,请标记这是正确的答案,如果它的工作。 –

+0

您可以使用某种while或if语句通过调用intro()和second()方法来循环,每个循环都会增加计数器变量。这将解决您的问题。 –

0

如何变计数器和可变introDone初始化?

我假设变量计数器在节目的开始具有值0,并且可变introDone是假的。如果是这样的话,则代码第二个部分不执行。例如:

public void hello(View view){ 
    // --> here, counter=0 and introDone=false 
    Toast toast = Toast.makeText(this, Integer.toString(counter), Toast.LENGTH_SHORT); 
    toast.show(); 
    intro(); // --> counter is still 0, so the variable introDone is still false 
    second(); // --> because introDone=false then the code is not executed 
    counter++; 
} 
+0

尝试查看更新的问题,我已经添加了初始的东西,哦,并通过它的工作方式在第一次点击 – FET