Выскакивает ошибка о том, что введено неправильное число - Java

Узнай цену своей работы

Формулировка задачи:

Я заранее извиняюсь за грубость. Я не хочу выслушивать крики людей, типа :Твой код не эффективен, КАК ТЫ ТАК ПЛОХО КОДИШь, и.т.д.
Листинг программы
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5. public class nabrosok {
  6. static class var {
  7. public static int a = 1;
  8. public static int b = 10;
  9. public static int c = a + (int) (Math.random() * b);
  10. public static int d = a + (int) (Math.random() * b);
  11. public static int result = c + d;
  12. public static String e = String.valueOf(c);
  13. public static String f = String.valueOf(d);
  14. public static String ress = String.valueOf(result);
  15. public static boolean bol = true;
  16. }
  17. public static void main (String[] args) {
  18. JPanel windowContent= new JPanel();
  19. FlowLayout fl = new FlowLayout();
  20. windowContent.setLayout(fl);
  21. JLabel lab = new JLabel();
  22. JButton b1 = new JButton("-");
  23. JButton b2 = new JButton("+");
  24. JButton b3 = new JButton("OK");
  25. JTextField edit = new JTextField(10);
  26. windowContent.add(b1);
  27. windowContent.add(b2);
  28. windowContent.add(lab);
  29. windowContent.add(edit);
  30. windowContent.add(b3);
  31. lab.setText(var.e + " + " + var.f + " = ");
  32. b1.addActionListener(new ActionListener() {
  33. public void actionPerformed(ActionEvent e) {
  34. var.bol = false;
  35. }
  36. }
  37. );
  38. b2.addActionListener(new ActionListener() {
  39. public void actionPerformed(ActionEvent e) {
  40. var.bol = true;
  41. }
  42. }
  43. );
  44. b3.addActionListener(new ActionListener() {
  45. public void actionPerformed(ActionEvent e) {
  46. if (edit.getText() == var.ress) {
  47. JOptionPane.showMessageDialog(windowContent, " Молодец, правильно! ");
  48. var.c = var.a + (int) (Math.random() + var.b);
  49. var.c = var.a + (int) (Math.random() + var.b);
  50. var.result = var.c + var.d;
  51. var.e = String.valueOf(var.c);
  52. var.f = String.valueOf(var.d);
  53. var.ress = String.valueOf(var.result);
  54. lab.setText(var.e + " + " + var.f + " = ");
  55. }
  56. else {
  57. JOptionPane.showMessageDialog(windowContent, " Не правильно, попробуй еще раз. ");
  58. }
  59. }
  60. }
  61. );
  62. JFrame frame = new JFrame("Посчитай-Ка");
  63. frame.setContentPane(windowContent);
  64. frame.setSize(400, 70);
  65. frame.setVisible(true);
  66. }
  67. }
Проблема вот в чем: когда я нажимаю на кнопку, при этом вводя правильный ответ в edit, выскакивает ошибка о том, что я ввел не правильное число. Заранее спасибо!

Решение задачи: «Выскакивает ошибка о том, что введено неправильное число»

textual
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class nabrosok {
  7.  
  8.     static class var {
  9.  
  10.         public static int a = 1;
  11.         public static int b = 10;
  12.  
  13.         public static int c = a + (int) (Math.random() * b);
  14.         public static int d = a + (int) (Math.random() * b);
  15.  
  16.         public static int result = c + d;
  17.  
  18.         public static String e = String.valueOf(c);
  19.         public static String f = String.valueOf(d);
  20.         public static String ress = String.valueOf(result);
  21.  
  22.         public static boolean bol = true;
  23.  
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.  
  28.         JPanel windowContent = new JPanel();
  29.  
  30.         FlowLayout fl = new FlowLayout();
  31.         windowContent.setLayout(fl);
  32.  
  33.         JLabel lab = new JLabel();
  34.         JButton b1 = new JButton("-");
  35.         JButton b2 = new JButton("+");
  36.         JButton b3 = new JButton("OK");
  37.  
  38.         JTextField edit = new JTextField(10);
  39.  
  40.         windowContent.add(b1);
  41.         windowContent.add(b2);
  42.         windowContent.add(lab);
  43.         windowContent.add(edit);
  44.         windowContent.add(b3);
  45.  
  46.         lab.setText(var.e + " + " + var.f + " = ");
  47.  
  48.         b1.addActionListener(new ActionListener() {
  49.                                  public void actionPerformed(ActionEvent e) {
  50.  
  51.                                      var.bol = false;
  52.  
  53.                                  }
  54.                              }
  55.         );
  56.  
  57.         b2.addActionListener(new ActionListener() {
  58.                                  public void actionPerformed(ActionEvent e) {
  59.  
  60.                                      var.bol = true;
  61.  
  62.                                  }
  63.                              }
  64.         );
  65.  
  66.         b3.addActionListener(new ActionListener() {
  67.                                  public void actionPerformed(ActionEvent e) {
  68.  
  69.                                      if (edit.getText().equals(var.ress)) {
  70.  
  71.                                          JOptionPane.showMessageDialog(windowContent, " Молодец, правильно! ");
  72.  
  73.                                          var.c = var.a + (int) (Math.random() + var.b);
  74.                                          var.c = var.a + (int) (Math.random() + var.b);
  75.  
  76.                                          var.result = var.c + var.d;
  77.                                          var.e = String.valueOf(var.c);
  78.                                          var.f = String.valueOf(var.d);
  79.                                          var.ress = String.valueOf(var.result);
  80.  
  81.                                          lab.setText(var.e + " + " + var.f + " = ");
  82.  
  83.                                      } else {
  84.                                          JOptionPane.showMessageDialog(windowContent, " Не правильно, попробуй еще раз. ");
  85.                                      }
  86.  
  87.                                  }
  88.                              }
  89.         );
  90.  
  91.         JFrame frame = new JFrame("Посчитай-Ка");
  92.         frame.setContentPane(windowContent);
  93.  
  94.         frame.setSize(400, 120);
  95.         frame.setVisible(true);
  96.  
  97.     }
  98.  
  99. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.833 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут