Выскакивает ошибка о том, что введено неправильное число - Java
Формулировка задачи:
Я заранее извиняюсь за грубость.
Я не хочу выслушивать крики людей, типа :Твой код не эффективен, КАК ТЫ ТАК ПЛОХО КОДИШь, и.т.д.
Проблема вот в чем: когда я нажимаю на кнопку, при этом вводя правильный ответ в edit, выскакивает ошибка о том, что я ввел не правильное число.
Заранее спасибо!
Листинг программы
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class nabrosok {
- static class var {
- public static int a = 1;
- public static int b = 10;
- public static int c = a + (int) (Math.random() * b);
- public static int d = a + (int) (Math.random() * b);
- public static int result = c + d;
- public static String e = String.valueOf(c);
- public static String f = String.valueOf(d);
- public static String ress = String.valueOf(result);
- public static boolean bol = true;
- }
- public static void main (String[] args) {
- JPanel windowContent= new JPanel();
- FlowLayout fl = new FlowLayout();
- windowContent.setLayout(fl);
- JLabel lab = new JLabel();
- JButton b1 = new JButton("-");
- JButton b2 = new JButton("+");
- JButton b3 = new JButton("OK");
- JTextField edit = new JTextField(10);
- windowContent.add(b1);
- windowContent.add(b2);
- windowContent.add(lab);
- windowContent.add(edit);
- windowContent.add(b3);
- lab.setText(var.e + " + " + var.f + " = ");
- b1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- var.bol = false;
- }
- }
- );
- b2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- var.bol = true;
- }
- }
- );
- b3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (edit.getText() == var.ress) {
- JOptionPane.showMessageDialog(windowContent, " Молодец, правильно! ");
- var.c = var.a + (int) (Math.random() + var.b);
- var.c = var.a + (int) (Math.random() + var.b);
- var.result = var.c + var.d;
- var.e = String.valueOf(var.c);
- var.f = String.valueOf(var.d);
- var.ress = String.valueOf(var.result);
- lab.setText(var.e + " + " + var.f + " = ");
- }
- else {
- JOptionPane.showMessageDialog(windowContent, " Не правильно, попробуй еще раз. ");
- }
- }
- }
- );
- JFrame frame = new JFrame("Посчитай-Ка");
- frame.setContentPane(windowContent);
- frame.setSize(400, 70);
- frame.setVisible(true);
- }
- }
Решение задачи: «Выскакивает ошибка о том, что введено неправильное число»
textual
Листинг программы
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class nabrosok {
- static class var {
- public static int a = 1;
- public static int b = 10;
- public static int c = a + (int) (Math.random() * b);
- public static int d = a + (int) (Math.random() * b);
- public static int result = c + d;
- public static String e = String.valueOf(c);
- public static String f = String.valueOf(d);
- public static String ress = String.valueOf(result);
- public static boolean bol = true;
- }
- public static void main(String[] args) {
- JPanel windowContent = new JPanel();
- FlowLayout fl = new FlowLayout();
- windowContent.setLayout(fl);
- JLabel lab = new JLabel();
- JButton b1 = new JButton("-");
- JButton b2 = new JButton("+");
- JButton b3 = new JButton("OK");
- JTextField edit = new JTextField(10);
- windowContent.add(b1);
- windowContent.add(b2);
- windowContent.add(lab);
- windowContent.add(edit);
- windowContent.add(b3);
- lab.setText(var.e + " + " + var.f + " = ");
- b1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- var.bol = false;
- }
- }
- );
- b2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- var.bol = true;
- }
- }
- );
- b3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (edit.getText().equals(var.ress)) {
- JOptionPane.showMessageDialog(windowContent, " Молодец, правильно! ");
- var.c = var.a + (int) (Math.random() + var.b);
- var.c = var.a + (int) (Math.random() + var.b);
- var.result = var.c + var.d;
- var.e = String.valueOf(var.c);
- var.f = String.valueOf(var.d);
- var.ress = String.valueOf(var.result);
- lab.setText(var.e + " + " + var.f + " = ");
- } else {
- JOptionPane.showMessageDialog(windowContent, " Не правильно, попробуй еще раз. ");
- }
- }
- }
- );
- JFrame frame = new JFrame("Посчитай-Ка");
- frame.setContentPane(windowContent);
- frame.setSize(400, 120);
- frame.setVisible(true);
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д