我目前正在研究一个随机数猜测游戏,我希望逻辑是这样的:
如果我回答,例如,正确答案“12”,如果我输入12(之前的答案),如何让程序拒绝答案?
这是我做的代码:
public void cmpans()
{
Object arr[]=set.toArray();
boolean success=false;
boolean boo=false;
String txget;
txget=gametext.getText();
String pars;
int ans;
pars=gametext.getText();
ans=Integer.parseInt(pars);
int cans[]=new int[10];
for(int i=0; i<10; i++)
{
if(ans==arr[i])
{
ck[i].setSelected(true);
gametext.setText("");
lblives.setText("life: "+life);
lbcons.setText("cons: "+cons);
userscore=userscore+10;
lbscore.setText("Score: "+userscore);
success=true;
}
else
{
boo=true;
gametext.setText("");
gametext.requestFocus(true);
}
arr[i]=null;
}
gametext.setText("");
gametext.requestFocus(true);
if(success==true)
{
cons++;
if(cons==3)
{
userscore=userscore+20;
lbscore.setText("Score: "+userscore);
lbachieve.setText("You're a GOOD GUESSER!");
lbcons.setText("cons: "+cons);
}
else if(cons==5)
{
userscore=userscore+50;
lbscore.setText("Score: "+userscore);
lbachieve.setText("You're an AMAZING GUESSER!!");
lbcons.setText("cons: "+cons);
}
}
else if(boo==true)
{
cons=0;
life--;
lblives.setText("life: "+life);
lbcons.setText("cons: "+cons);
}
}
提前感谢所有有帮助的人!