我的代码运行良好而没有错误,但程序假设你说你是等于,少于或大于计算机生成的数字(双倍),而且当我输入1或1.0时,计算机会生成a 2甚至有时候4,它说我是平等的,我不知所措,甚至不知道从哪里开始发现什么是错的。我得到的唯一线索是舍入可能会影响它,因为它正如你在代码中看到的那样四舍五入。
String overOrUnder = "Over or Under?";
String CompNumberString = "";
double compNumber;
String userNumberString = "";
double userNumber;
public void actionPerformed(ActionEvent e)
{
generateCompNumber();
labelCompNumber.setText(String.valueOf(compNumber));
labelCompNumber.revalidate();
getUserInputToInteger();
labelUserNumber.revalidate();
labelUserNumber.setText(userNumberString);
overOrUnderOrEqual();
}
});
}
public void generateCompNumber()
{
compNumber = Math.random() * 5;
compNumber = Math.round(compNumber * 1);
compNumber = compNumber/1;
}
public void getUserInputToInteger()
{
userNumberString = jTextYourNumber.getText();
userNumber = Double.parseDouble(userNumberString);
}
public void overOrUnderOrEqual()
{
labelOverOrUnder.setText(overOrUnder);
if (compNumber == userNumber)
{
equal = true;
overOrUnder = "RIGHT ON!";
}
else if (compNumber > userNumber)//comp greater user
{
equal = false;
overOrUnder = "You was Under!";
}
else if (compNumber < userNumber)//comp less user
{
equal = false;
overOrUnder = "You was Over!";
}
}
答案 0 :(得分:0)
您的比较部分代码工作正常。只有错误是,你在评估字符串之前设置文本。 labelOverOrUnder.setText(overOrUnder);
应该在if-else语句之后。这就是你得到错误信息的原因。这是修改后的代码。
class GMNR extends JFrame
{
String overOrUnder = "Over or Under?";
String CompNumberString = "";
double compNumber;
String userNumberString = "";
double userNumber;
Boolean equal;
JPanel panelMain = new JPanel();
JTextField jTextYourNumber = new JTextField(25);
JButton buttonGenerate = new JButton("Generate");
JLabel labelCompNumber = new JLabel();
JLabel labelUserNumber = new JLabel();
JLabel labelOverOrUnder = new JLabel();
public GMNR()
{
//variables
//end of variables
setTitle("Guess My Number!");
setVisible(true);
setSize(1200, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panelMain.add(jTextYourNumber);
jTextYourNumber.setBackground(Color.green);
panelMain.setBackground(Color.yellow);
panelMain.add(jTextYourNumber);
panelMain.add(labelCompNumber);
panelMain.add(labelUserNumber);
panelMain.add(buttonGenerate);
panelMain.add(labelOverOrUnder);
add(panelMain);
labelOverOrUnder.revalidate();
buttonGenerate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
generateCompNumber();
labelCompNumber.setText(String.valueOf(compNumber));
labelCompNumber.revalidate();
getUserInputToInteger();
overOrUnderOrEqual();
labelUserNumber.setText(userNumberString);
labelUserNumber.revalidate();
}
});
}
public void generateCompNumber()
{
compNumber = Math.random() * 5;
compNumber = Math.round(compNumber * 1);
compNumber = compNumber/1;
}
public void getUserInputToInteger()
{
userNumberString = jTextYourNumber.getText();
userNumber = Double.parseDouble(userNumberString);
}
public void overOrUnderOrEqual()
{
if (compNumber == userNumber)
{
equal = true;
overOrUnder = "RIGHT ON!";
}
else if (compNumber > userNumber)//comp greater user
{
equal = false;
overOrUnder = "You was Under!";
}
else if (compNumber < userNumber)//comp less user
{
equal = false;
overOrUnder = "You was Over!";
}
labelOverOrUnder.setText(overOrUnder);
System.out.println("compNumber "+compNumber+", userNumber "+userNumber+", "+overOrUnder);
}
}