为什么即使计算是正确的,方法仍返回nan?

4xrmg8kj  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(458)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

13天前关门了。
改进这个问题
我试图用两个不同的jtextfield来制作一个gui,计算一个egfr值需要这个输入。在方法中打印egfr时 calcAndSetTotal() ,我得到了正确的计算结果(egfr的值),但是当我返回egfr并调用 calcAndSetTotal() 在main中并尝试打印它,即使我将所有输入值都传递到double中,也会得到值“egfr=nan”。

  1. package NyreFunktion;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextField;
  5. import java.awt.Color;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. public class calculation {
  9. public JFrame frame;
  10. public JTextField textField;
  11. public JTextField textField_1;
  12. public double højde;
  13. public double kreatinin;
  14. double eGFR=0;
  15. public static void main(String[] args) throws InterruptedException {
  16. EventQueue.invokeLater(new Runnable() {
  17. public void run() {
  18. try {
  19. calculation window = new calculation();
  20. window.frame.setVisible(true);
  21. double value=window.eGFR;
  22. System.out.println(value);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. });
  28. calculation cl= new calculation();
  29. cl.initialize();
  30. cl.calcAndSetTotal();
  31. System.out.println("test=" +cl.calcAndSetTotal());
  32. }
  33. public calculation() {
  34. initialize();
  35. }
  36. public void initialize() {
  37. frame = new JFrame();
  38. frame.getContentPane().setBackground(Color.WHITE);
  39. frame.setBounds(100, 100, 450, 450);
  40. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. frame.getContentPane().setLayout(null);
  42. textField = new JTextField();
  43. textField.setBounds(121, 114, 130, 26);
  44. frame.getContentPane().add(textField);
  45. textField.addActionListener(new ActionListener() {
  46. public void actionPerformed(ActionEvent event) {
  47. højde =Double.parseDouble(textField.getText());
  48. System.out.println("Højde: " + højde);
  49. calcAndSetTotal();
  50. }
  51. });
  52. textField_1 = new JTextField();
  53. textField_1.setBounds(121, 188, 130, 26);
  54. frame.getContentPane().add(textField_1);
  55. textField_1.setColumns(10);
  56. textField_1.addActionListener(new ActionListener() {
  57. public void actionPerformed(ActionEvent eventt) {
  58. kreatinin =Double.parseDouble(textField_1.getText());
  59. System.out.println("kreatinin: " + kreatinin);
  60. calcAndSetTotal();
  61. }
  62. });
  63. }
  64. double calcAndSetTotal() {
  65. eGFR = højde/kreatinin;
  66. System.out.println("GFR" + " " + "=" + " " + eGFR);
  67. return eGFR;
  68. }
  69. }
gwo2fgha

gwo2fgha1#

(我假设在您的真实代码中 public class calculation { 后一行 import 声明。)
真正的问题是您的代码使用了 calculation 班级。
第一个 calculation 在中创建的示例 run() 方法并将其设置为可见。
第二个 calculation 在中创建的示例 main 方法本身,不要将其设置为可见。
第一个示例是在gui中输入文本时正在更新的示例,等等。
第二个示例是您用来获取 eGFR 你正在打印的。但由于这个示例从来都不可见:
无法调用其文本框的操作侦听器,因此
højde 以及 kreatinin 字段不能更改其默认值;即 0.0 以及 0.0 ,因此
你计算 0.0 / 0.0 因此 NaN 在中写入标准输出 main .

oug3syen

oug3syen2#

你的代码有点问题。
首先,我认为你在计算中犯了一个错误,忘记了计算 public calculation() 使用类定义

  1. public class calculation {
  2. public JFrame frame;
  3. public JTextField textField;
  4. public JTextField textField_1;
  5. public double højde = 1;
  6. public double kreatinin = 1;
  7. double eGFR = 0;
  8. public calculation() {
  9. initialize();
  10. }
  11. public void initialize() {...}
  12. double calcAndSetTotal() {...}
  13. }

你不需要打电话

  1. calculation cl= new calculation();
  2. cl.initialize();
  3. cl.calcAndSetTotal();
  4. System.out.println("test=" +cl.calcAndSetTotal());

现在有两个示例运行在同一个类中而不可见。这也是导致 NaN 因为你被零除了。我可以给你一个提示,就是检查用户输入的除法。
希望这有帮助

编辑

在对问题有了更好的理解之后,这更像是一种行为

  1. public class calculation {
  2. public JFrame frame;
  3. public JTextField textField;
  4. public JTextField textField_1;
  5. /*
  6. when calling calcAndSetTotal() without changing this
  7. values first you will get a Nan since højde, kreatinin
  8. are both initialised with zero
  9. */
  10. public double højde;
  11. public double kreatinin;
  12. double eGFR=0;
  13. static calculation window;
  14. public static void main(String[] args) throws InterruptedException {
  15. ((Runnable) () -> {
  16. try {
  17. window = new calculation();
  18. window.frame.setVisible(true);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }).run();
  23. /*
  24. Will output NaN since højde, kreatinin are still zero
  25. */
  26. System.out.println(window.calcAndSetTotal());
  27. }
  28. public calculation() {...}
  29. public void initialize() {...}
  30. double calcAndSetTotal() {...}
  31. }
展开查看全部

相关问题