gpt4 book ai didi

当我尝试进行计算时,Java 无法识别 double

转载 作者:行者123 更新时间:2023-12-01 23:56:51 27 4
gpt4 key购买 nike

因此,对于这个程序,我必须使用 java GUI 来获取温度和风速,然后从那里计算 Windchill。我这样做是为了当用户单击“计算 Windchill”按钮时,它应该使用我输入的公式来计算。但我不断收到有关此 double 的各种错误,我不知道为什么。您可以在 actionPerformed 部分找到错误,是“windChillInt”出现此错误。谢谢。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Windchill extends JFrame implements ActionListener{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private String degree;
private String wind;
private int degreeInt;
private int windInt;
private double windChillInt;
private JButton windButton;
private JLabel prompt;
private JLabel prompt1;
private JTextField inputLine;
private JTextField inputLine1;

public Windchill(){
setTitle("Windchill");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
inputLine = new JTextField();
inputLine.setColumns(3);
inputLine.addActionListener(this);
contentPane.add(inputLine);
prompt = new JLabel();
prompt.setText("Enter the degrees in Farienheight ");
prompt.setSize(150,25);
contentPane.add(prompt);
inputLine1 = new JTextField();
inputLine1.setColumns(3);
inputLine1.addActionListener(this);
contentPane.add(inputLine1);

prompt1 = new JLabel();
prompt1.setText("Enter the wind speed in MPH");
prompt1.setSize(150,25);
contentPane.add(prompt1);

windButton = new JButton("Calculate windchill");
contentPane.add(windButton);
windButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if (event.getSource() instanceof JButton){
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == windButton){
degree = inputLine.getText();
degreeInt = Integer.parseInt(degree);
wind = inputLine1.getText();
windInt = Integer.parseInt(wind);
windChillInt = 0.08(degreeInt - 91.4) (3.71*math.sqrt(windInt) + 5.81 - 0.25 *windInt) + 91.4;

}

}
}
public static void main(String[] args) {
Windchill frame;
frame = new Windchill();
frame.setVisible(true);
}
}

好的,谢谢大家,我已经成功了,但现在我遇到了另一个问题,它不会像我想要的那样在 GUI 上显示新的 WindChillInt。这是我把它改成的。

public void actionPerformed(ActionEvent event){
if (event.getSource() instanceof JButton){
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == windButton){
degree = inputLine.getText();
degreeInt = Integer.parseInt(degree);
wind = inputLine1.getText();
windInt = Integer.parseInt(wind);
windChillInt = 0.08 * (degreeInt - 91.4)*(3.71* (Math.sqrt(windInt)) + 5.81 - 0.25 *windInt) + 91.4;

prompt2 = new JLabel();
prompt2.setText("The windchill is " + windChillInt);
prompt2.setSize(150,25);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(prompt2);
}

最佳答案

我相信问题出在这一行:

windChillInt = 0.08(degreeInt - 91.4)        (3.71 * math.sqrt(windInt) + 5.81 - 0.25 *windInt) + 91.4;
<小时/>

windChillInt = 0.08(degreeInt - 91.4)应该是windChillInt = 0.08 * (degreeInt - 91.4)

然后,您需要乘以 windChillInt = 0.08 * (degreeInt - 91.4)(3.71 * math.sqrt(windInt) + 5.81 - 0.25 *windInt) + 91.4一起,或者搬家(3.71 * math.sqrt(windInt) + 5.81 - 0.25 *windInt) + 91.4分解为它自己的语句并将其分配给某些东西。

关于当我尝试进行计算时,Java 无法识别 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com