无法在循环中生成随机数 [Java]

huangapple go评论78阅读模式
英文:

Cannot draw random number in loop [Java]

问题

以下是您提供的代码的翻译:

public void randomCPUShot(){

    choose = (int) ((Math.random() * 9) + 1);
    System.out.println(choose);

    switch (choose){
        case 1: checkButton(button1); break;
        case 2: checkButton(button2); break;
        case 3: checkButton(button3); break;
        case 4: checkButton(button4); break;
        case 5: checkButton(button5); break;
        case 6: checkButton(button6); break;
        case 7: checkButton(button7); break;
        case 8: checkButton(button8); break;
        case 9: checkButton(button9); break;
        default: break;
    }
}

public void checkButton(JButton button) {
    String text = button.getText();

    if (text.equals("X")) {
        randomCPUShot();
    } else if (text.equals("O")) {
        randomCPUShot();
    } else {
        symbolChart = 'O';
        switch (choose) {
            case 1: button1.setText("" + symbolChart); button1.setEnabled(false); break;
            case 2: button2.setText("" + symbolChart); button2.setEnabled(false); break;
            case 3: button3.setText("" + symbolChart); button3.setEnabled(false); break;
            case 4: button4.setText("" + symbolChart); button4.setEnabled(false); break;
            case 5: button5.setText("" + symbolChart); button5.setEnabled(false); break;
            case 6: button6.setText("" + symbolChart); button6.setEnabled(false); break;
            case 7: button7.setText("" + symbolChart); button7.setEnabled(false); break;
            case 8: button8.setText("" + symbolChart); button8.setEnabled(false); break;
            case 9: button9.setText("" + symbolChart); button9.setEnabled(false); break;
            default: break;
        }
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    switch (command) {
        case "b1": button1.setEnabled(false); setSymbol(); button1.setText("" + symbolChart); randomCPUShot(); break;
        case "b2": button2.setEnabled(false); setSymbol(); button2.setText("" + symbolChart); randomCPUShot(); break;
        case "b3": button3.setEnabled(false); setSymbol(); button3.setText("" + symbolChart); randomCPUShot(); break;
        case "b4": button4.setEnabled(false); setSymbol(); button4.setText("" + symbolChart); randomCPUShot(); break;
        case "b5": button5.setEnabled(false); setSymbol(); button5.setText("" + symbolChart); randomCPUShot(); break;
        case "b6": button6.setEnabled(false); setSymbol(); button6.setText("" + symbolChart); randomCPUShot(); break;
        case "b7": button7.setEnabled(false); setSymbol(); button7.setText("" + symbolChart); randomCPUShot(); break;
        case "b8": button8.setEnabled(false); setSymbol(); button8.setText("" + symbolChart); randomCPUShot(); break;
        case "b9": button9.setEnabled(false); setSymbol(); button9.setText("" + symbolChart); randomCPUShot(); break;
        default: break;
    }
    repaint();
}

public void setSymbol() {
    symbolChart = 'X';
}

如果您有任何其他问题或需要进一步的帮助,请随时问我。

英文:

I am trying to make Tic Tak Toe Game but I got problem that I cannot handle.

When i make choice it set Text it as X and also Not Editable. Then i generate random number from 1-9 and
by switch i go to another method checkButton() where I take text of button and if text is X or O , It should do random number again.

But actually whats work is that It change empty char into O but if Its X or O then It doesnt generate new number but only change those X or O to O. (Sometimes it makes my choice as O)

I dont know where I should change something.It may be problem outside those two methods so I am adding a pastebin link to see : https://pastebin.com/n80x9xSH

public void randomCPUShot(){
choose=(int)((Math.random()*9)+1);
System.out.println(choose);
switch (choose){
case 1: checkButton(button1); break;
case 2: checkButton(button2); break;
case 3: checkButton(button3); break;
case 4: checkButton(button4); break;
case 5: checkButton(button5); break;
case 6: checkButton(button6); break;
case 7: checkButton(button7); break;
case 8: checkButton(button8); break;
case 9: checkButton(button9); break;
default: break;
} 
}
public void checkButton(JButton button)
{
String text = button.getText();
if(text=="X"){ randomCPUShot();
// mam blad taki ze oki losuje miejsce dla O ale jak jest to X no to zamienia X na 0
}
else if(text=="O"){
randomCPUShot();
}else
{
symbolChart='O';
switch (choose){
case 1: button1.setText(""+symbolChart); button1.setEnabled(false); break;
case 2: button2.setText(""+symbolChart);button2.setEnabled(false);break;
case 3: button3.setText(""+symbolChart);button3.setEnabled(false);break;
case 4: button4.setText(""+symbolChart);button4.setEnabled(false);break;
case 5: button5.setText(""+symbolChart);button5.setEnabled(false);break;
case 6: button6.setText(""+symbolChart);button6.setEnabled(false);break;
case 7: button7.setText(""+symbolChart);button7.setEnabled(false);break;
case 8: button8.setText(""+symbolChart);button8.setEnabled(false);break;
case 9: button9.setText(""+symbolChart);button9.setEnabled(false);break;
default:break;
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command){
case "b1": button1.setEnabled(false); setSymbol(); button1.setText(""+symbolChart); randomCPUShot(); break;
case "b2": button2.setEnabled(false); setSymbol(); button2.setText(""+symbolChart); randomCPUShot(); break;
case "b3": button3.setEnabled(false); setSymbol(); button3.setText(""+symbolChart); randomCPUShot(); break;
case "b4": button4.setEnabled(false); setSymbol(); button4.setText(""+symbolChart); randomCPUShot(); break;
case "b5": button5.setEnabled(false); setSymbol(); button5.setText(""+symbolChart); randomCPUShot(); break;
case "b6": button6.setEnabled(false); setSymbol(); button6.setText(""+symbolChart); randomCPUShot(); break;
case "b7": button7.setEnabled(false); setSymbol(); button7.setText(""+symbolChart); randomCPUShot(); break;
case "b8": button8.setEnabled(false); setSymbol(); button8.setText(""+symbolChart); randomCPUShot(); break;
case "b9": button9.setEnabled(false); setSymbol(); button9.setText(""+symbolChart); randomCPUShot(); break;
default :break;
}
repaint();
}
public void setSymbol() {
symbolChart='X';
}

答案1

得分: 1

你正在使用 == 进行字符串比较。绝对不要这样做。请使用 equals()。此外,当将一个 String 实例与一个常量字符串值进行比较时,应在该常量值上调用 equals()。在你的情况下,你应该这样做:"X".equals(text),诸如此类。

英文:

You are comparing strings using ==. Never do that. Use equals(). Also, when comparing an instance of String to a constant string value, call equals() on that constant value. In your case, you should be doing things like "X".equals(text) and so on.

huangapple
  • 本文由 发表于 2020年8月27日 21:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63616978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定