ошибка The operator <= is undefined for the argument type(s) darts, int
Я столкнулся с одной проблемой. Когда я сравниваю числа в if выходит ошибка: The operator <= is undefined for the argument type(s) darts, int.
Вот код if:
if (d1 <= 1) {
JOptionPane.showMessageDialog(null, "You have earned 1 point!");
} else if (d1 <= 3) {
JOptionPane.showMessageDialog(null, "You have earned 3 point!");
} else if (d1 <= 5) {
JOptionPane.showMessageDialog(null, "You have earned 5 point!");
} else {
JOptionPane.showMessageDialog(null, "You lost!");
}
Весь код:
JButton enterButton = new JButton("Enter");
enterButton.setBounds(239, 239, 97, 25);
contentPane.add(enterButton);
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String oneThrow = firstThrow.getText().trim();
String twoThrow = secondThrow.getText().trim();
System.out.println(oneThrow + " " + twoThrow);
double x = Double.parseDouble(oneThrow);
double y = Double.parseDouble(twoThrow);
darts d1 = new darts(x, y);
if(oneThrow.isEmpty() || twoThrow.isEmpty()) {
JOptionPane.showMessageDialog(null, "Пожалуйста введите значения в поля.");
}
if (d1 <= 1) {
JOptionPane.showMessageDialog(null, "You have earned 1 point!");
} else if (d1 <= 3) {
JOptionPane.showMessageDialog(null, "You have earned 3 point!");
} else if (d1 <= 5) {
JOptionPane.showMessageDialog(null, "You have earned 5 point!");
} else {
JOptionPane.showMessageDialog(null, "You lost!");
}
}
});
Код darts(), который я использовал в вышеупомянутом коде:
public class darts {
private double res;
public darts(double x, double y) {
res = x + y;
}
Как исправить эту ошибку? Спасибо заранее.
Ответы (1 шт):
Автор решения: AlekseiGaile
→ Ссылка
public class darts {
private double res;
public darts(double x, double y) {
res = x + y;
}
public double getRes() {
return res;
}
}
И сравнивайте - (d1.getRes() <= 1)
Классы и конструкторы принято писать с большой буквы, рекомендую переименовать darts в Darts.