我正在创建一个游戏,其中我创建了一个包含一个字母的字符串,并且如果在选择了随机字母时单击了按钮,则希望发生某些事情。问题在于.equals()
,我还尝试将String
数组变量更改为char
数组,并使用==
同样的问题:
public class JeuxMemoire extends Applet implements ActionListener {
Button start;
Button end;
Button un= new Button("q");
Button deux= new Button("w");
Button trois= new Button("e");
Button quatre= new Button("r");
Button cinq= new Button("t");
Button six= new Button("y");
Button sept= new Button("u");
Button huit= new Button("i");
Button neuf= new Button("o");
Button dix= new Button("p");
Button onze= new Button("a");
Button douze= new Button("s");
Button treize= new Button("d");
Button quatorze= new Button("f");
Button quinze= new Button("g");
Button seize= new Button("h");
int scur = 0;
String[] num = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h"};
Label score;
Label timer = new Label("Lettre: ") ;
Label lettre = new Label("temps: ");
public void RestartGame()
{
start.setEnabled(true);
un.setEnabled(false);
deux.setEnabled(false);
trois.setEnabled(false);
quatre.setEnabled(false);
cinq.setEnabled(false);
six.setEnabled(false);
sept.setEnabled(false);
huit.setEnabled(false);
neuf.setEnabled(false);
dix.setEnabled(false);
onze.setEnabled(false);
douze.setEnabled(false);
treize.setEnabled(false);
quatorze.setEnabled(false);
quinze.setEnabled(false);
seize.setEnabled(false);
scur = 0;
}
public String populateStringArray()
{
int ran = (int) (Math.random() * 15);
//System.out.println(ran);
return num[ran];
}
public void init(){
this.setLayout(new BorderLayout());
this.setBackground(Color.ORANGE);
Font policeApplet = new Font("ComicSans", Font.BOLD, 20);
this.setFont(policeApplet);
start = new Button ("Commence");
start.addActionListener(this);
Panel superior = new Panel();
superior.setLayout(new BorderLayout());
superior.add(start);
this.add(superior,"North");
Panel time = new Panel();
time.add(timer);
superior.add(time,"East");
Panel lett = new Panel();
lett.add(lettre);
superior.add(lett,"West");
Panel central = new Panel();
central.setLayout(new GridLayout(4, 4));
this.add(central, "Center");
central.add(un);
central.add(deux);
central.add(trois);
central.add(quatre);
central.add(cinq);
central.add(six);
central.add(sept);
central.add(huit);
central.add(neuf);
central.add(dix);
central.add(onze);
central.add(douze);
central.add(treize);
central.add(quatorze);
central.add(quinze);
central.add(seize);
RestartGame();
score = new Label ("Votre Score est " + scur);
this.add(score,"South");
un.addActionListener(this);
deux.addActionListener(this);
trois.addActionListener(this);
quatre.addActionListener(this);
cinq.addActionListener(this);
six.addActionListener(this);
sept.addActionListener(this);
huit.addActionListener(this);
neuf.addActionListener(this);
dix.addActionListener(this);
onze.addActionListener(this);
douze.addActionListener(this);
treize.addActionListener(this);
quatorze.addActionListener(this);
quinze.addActionListener(this);
seize.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
String lettres ="";// populateStringArray();
//Button Buton = (Button) event.getSource();
Button buton = (Button) event.getSource();
if (buton == start){
lettres = populateStringArray();
timer.setText("Let:" + lettres);
start.setEnabled(false);
un.setEnabled(true);
deux.setEnabled(true);
trois.setEnabled(true);
quatre.setEnabled(true);
cinq.setEnabled(true);
six.setEnabled(true);
sept.setEnabled(true);
huit.setEnabled(true);
neuf.setEnabled(true);
dix.setEnabled(true);
onze.setEnabled(true);
douze.setEnabled(true);
treize.setEnabled(true);
quatorze.setEnabled(true);
quinze.setEnabled(true);
seize.setEnabled(true);
}
else if ((buton == un)&&(lettres.equals("q"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if ((buton == deux)&&(lettres.equals("w"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == trois)/*&&(lettres.equals("e"))*/){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if ((buton == quatre)&&(lettres.equals("r"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == cinq)&&(lettres.equals("t"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == six)&&(lettres.equals("y"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == sept)&&(lettres.equals("u"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == huit)&&(lettres.equals("i"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == neuf)&&(lettres.equals("o"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == dix)&&(lettres.equals("p"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == onze)&&(lettres.equals("a"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == douze)&&(lettres.equals("s"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == treize)&&(lettres.equals("d"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == quatorze)&&(lettres.equals("f"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == quinze)&&(lettres.equals("g"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
else if((buton == seize)&&(lettres.equals("h"))){
scur++;
lettres = populateStringArray();
System.out.println(lettres);
timer.setText("Lettre:" + lettres);
score.setText("Score: "+ scur);
}
//else System.out.println(lettres);
else {
lettres = populateStringArray();
System.out.println(lettres);
}
}
}
答案 0 :(得分:1)
您的问题是,只要触发此动作侦听器,lettres就会始终初始化为空字符串“”,因此equals()方法将始终检查空字符串。其他任何if语句都不能成立。