Java 在之前工作但在之后不工作的循环
我有一个奇怪的问题。我对三个按钮有相同的代码,但对第一个按钮有效,对其他按钮无效。怎么可能呢?有人能给我解释一下吗?非常感谢。 我可以点击按钮(B和C),但不能点击两次。发生什么事?在第一种情况下(按钮A),它工作,但它只是显示一张卡,而不是两张Java 在之前工作但在之后不工作的循环,java,android,eclipse,Java,Android,Eclipse,我有一个奇怪的问题。我对三个按钮有相同的代码,但对第一个按钮有效,对其他按钮无效。怎么可能呢?有人能给我解释一下吗?非常感谢。 我可以点击按钮(B和C),但不能点击两次。发生什么事?在第一种情况下(按钮A),它工作,但它只是显示一张卡,而不是两张 @SuppressLint("Recycle") public class GameActivity extends Activity { protected View buttonA; protected View buttonB;
@SuppressLint("Recycle")
public class GameActivity extends Activity {
protected View buttonA;
protected View buttonB;
protected View buttonC;
protected View buttonD;
final int randomInt1 = 0;
final int randomInt2 = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
findViewById(R.id.A);
Resources res = getResources();
String[] StringDeck = getResources().getStringArray(R.array.StringDeck);
int[] DeckDraw = getResources().getIntArray(R.array.DeckDraw);
final HashMap<String, Integer> DECK = new HashMap<String, Integer>();
for(int i = 0; i < DeckDraw.length; i++){
DECK.put(StringDeck[i], DeckDraw[i]);
}
final TypedArray Deck = res.obtainTypedArray(R.array.DeckDraw);
final ViewGroup groupButtons1 = (ViewGroup)findViewById(R.id.linearLayout1);
View v1;
final Button backgroundA = (Button) findViewById(R.id.A);
final Button backgroundB = (Button) findViewById(R.id.B);
final Button backgroundC = (Button) findViewById(R.id.C);
final Button backgroundD = (Button) findViewById(R.id.D);
final Button backgroundE = (Button) findViewById(R.id.E);
final Random random1 = new Random();
final Random random2 = new Random();
for(int i = 0; i < groupButtons1.getChildCount(); i++) {
v1 = groupButtons1.getChildAt(i);
if(v1 instanceof Button) v1.setOnClickListener(new Button.OnClickListener() {
@SuppressLint("CutPasteId") //Legato ai button inferiori
public void onClick(View view){
backgroundA.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(a < 2){
int randomInt1 = random1.nextInt(Deck.length());
int drawableIDA = Deck.getResourceId(randomInt1, -1);
backgroundA.setBackgroundResource(drawableIDA);
a ++;
}
}
});
backgroundB.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if (b < 2){
int randomInt2 = random1.nextInt(Deck.length());
int drawableIDB = Deck.getResourceId(randomInt2, -1);
backgroundB.setBackgroundResource(drawableIDB);
b ++;
}
backgroundB.setEnabled(false);
}
});
backgroundC.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if (c < 2){
int randomInt3 = random1.nextInt(Deck.length());
int drawableIDC = Deck.getResourceId(randomInt3, -1);
backgroundC.setBackgroundResource(drawableIDC);
c ++;
}
backgroundC.setEnabled(false);
}
});
@suppressint(“回收”)
公共类GameActivity扩展活动{
保护视图按钮;
保护视图按钮b;
保护视图按钮;
保护视图按钮;
最终int randomInt1=0;
最终int randomInt2=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_游戏);
findViewById(R.id.A);
Resources res=getResources();
String[]StringDeck=getResources().getStringArray(R.array.StringDeck);
int[]DeckDraw=getResources().getIntArray(R.array.DeckDraw);
最终HashMap组=新HashMap();
对于(int i=0;i
很抱歉缩进了您的程序正在存储
a
,b
,c
的值,但是对它们所做的唯一更改是使它们递增。单击几下后,a
,b
,c
将高于2,并且永远无法低于2。您是否重置了>a
什么时候需要重置?我不明白你的问题你从来没有将变量a、b、c重置为0,所以如果你按下它们,它们就会跳过循环。是的,但我应该在循环停止之前按下它们两次很抱歉缩进
修复它会更有用耶,我不需要它们在2下。我只需要案例a=0,a=1。然后停下来。我不必再给他们打电话了