gpt4 book ai didi

java - 类型不匹配 : Cannot Convert From Void to String in Java

转载 作者:行者123 更新时间:2023-12-02 12:14:53 24 4
gpt4 key购买 nike

我正在为类(class)创建一个程序,该程序使用公式、ifelse、变量以及用于消息和输入的对话框来复制河流流量的曼宁计算器。我已经创建了大部分程序,即公式、变量、一些 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);

这给了我标题中所述的错误消息。我不确定是什么会导致这个问题,这是我的代码中唯一的串联部分,我想快速输入它,这样以后就不必担心它。连接中的一些变量尚未制定,但在我看来,仍然不应该引发错误。我的整个程序如下:

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);

}
}

任何有关此问题的帮助将不胜感激。

最佳答案

改变

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 返回类型)。因此,您无法将此类调用的值分配给变量。

关于java - 类型不匹配 : Cannot Convert From Void to String in Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260727/

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