我正在为类创建一个程序,使用公式,ifelse,变量和消息和输入的对话框复制Mannings计算器的河流流量。我已经创建了大部分程序,即公式,变量,一些ifelse语句,但是我遇到了一个与将resultMessage变量显示为JOptionPane消息框有关的问题。我将几个Double类型变量的输出与一些字符串混合在一起,这就是它的样子:
resultMessage=JOptionPane.showMessageDialog(null, "At a depth of "+enteredDepth+"ft the flow is "+ calculatedFlow+" cubic ft per second."+"\n"+ "Difference:"+difference);
这给了我标题中所述的错误消息。我不确定会导致这种情况,它是我代码中唯一连接的部分,我想快速输入,因此以后不必担心。连接中的一些变量尚未制定,但仍然不应该抛出错误,IMO。我的整个计划如下:
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class ManningCalculator {
public static void main(String[] args) {
DecimalFormat df=new DecimalFormat("##.00");
//Assigned Constants
final double MANNING_COEFFICIENT = 1.486;
final double COEFFICIENT_BRICK = 0.015;
final double COEFFICIENT_CONCRETE=0.012;
final double COEFFICIENT_ASPHALT= 0.016;
final double COEFFICIENT_GRAVEL= 0.023;
final String MATERIAL_BRICK= "Brick";
final String MATERIAL_CONCRETE="Concrete";
final String MATERIAL_ASPHALT="Asphalt";
final String MATERIAL_GRAVEL="Gravel";
final double TOLERANCE= 0.01;
//input Variables
int attemptNumber;
double desiredFlow;
double maxDepth;
double width;
double roughnessCoefficient;
double deltaHeight=0;
double deltaLength=0;
double enteredDepth;
double calculatedFlow;
double difference;
double minError;
String resultMessage="";
//Math variables
double channelVelocity=0;
double hydraulicRadius=0;
double channelArea=0;
double channelSlope=0;
double wettedPerimeter=0;
//JPane for input variables
desiredFlow=Double.parseDouble(JOptionPane.showInputDialog("Enter desired flow"));
maxDepth= Double.parseDouble(JOptionPane.showInputDialog("Enter the maximum depth"));
width= Double.parseDouble(JOptionPane.showInputDialog("Enter the width"));
roughnessCoefficient=Double.parseDouble(JOptionPane.showInputDialog("Enter choice for roughness coefficient:"+
"\n"+"1 Brick"+"\n"+"2 Concrete"+"\n"+"3 Asphalt"+"\n"+"4 Gravel"));
deltaHeight=Double.parseDouble(JOptionPane.showInputDialog("Enter the change in channel height"));
deltaLength=Double.parseDouble(JOptionPane.showInputDialog("Enter the change in channel length"));
enteredDepth=Double.parseDouble(JOptionPane.showInputDialog("Attempt1"+"\n"+"Enter a depth:"));
//If else if statements
if(roughnessCoefficient==1) {
roughnessCoefficient=COEFFICIENT_BRICK;
}else if(roughnessCoefficient==2) {
roughnessCoefficient=COEFFICIENT_CONCRETE;
}else if(roughnessCoefficient==3) {
roughnessCoefficient=COEFFICIENT_ASPHALT;
}else {
roughnessCoefficient=COEFFICIENT_GRAVEL;
};
if(desiredFlow<0 ) {
JOptionPane.showInputDialog("Desired flow must be greater than 0");
}else {
JOptionPane.showMessageDialog(null, "Desired flow not valid."+"\n"+"The program terminates");
};
if(enteredDepth<0.0) {
if(enteredDepth>maxDepth) {
JOptionPane.showMessageDialog(null, "Entered depth is not valid."+"\n"+"Desired depth is set to 10.0");
enteredDepth=maxDepth;
}
}
//Math Formulas
desiredFlow=channelVelocity * channelArea;
channelVelocity= (MANNING_COEFFICIENT /roughnessCoefficient) *
Math.pow(hydraulicRadius, (2/3)) *Math.pow(channelSlope, (1/2));
hydraulicRadius =channelArea/wettedPerimeter;
channelSlope=deltaHeight/deltaLength;
resultMessage=JOptionPane.showMessageDialog(null, "At a depth of "+enteredDepth+"ft the flow is "+
calculatedFlow+" cubic ft per second."+"\n"+ "Difference:"+difference);
System.exit(0);
}
}
对此问题的任何帮助将不胜感激。
答案 0 :(得分:0)
更改
resultMessage = JOptionPane.showMessageDialog(null, "At a depth of + enteredDepth + "ft the flow is " + calculatedFlow + " cubic ft per second." + "\n" + "Difference:" + difference);
到
JOptionPane.showMessageDialog(null, "At a depth of " + enteredDepth + "ft the flow is " + calculatedFlow+" cubic ft per second." + "\n" + "Difference:" + difference);
问题是JOptionPane.showMessageDialog
没有返回值(用说它有void
返回类型)。因此,您无法将此类调用的值分配给变量。