我正在使用公式,ifelse,變量和對話框爲消息和輸入複製Mannings Calculator for river flow類。我創建了大部分程序,即公式,變量,一些ifelse語句,但是我有一個與顯示resultMessage變量作爲JOptionPane消息框相關的問題。我串接輸出一些字符串混合幾種雙師型變量,這是它的樣子:類型不匹配:無法在Java中將void從void轉換爲String
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);
}
}
任何有關這個問題的幫助將不勝感激。
請顯示確切的錯誤,並附有堆棧跟蹤。另外,請嘗試格式化您的代碼。突出顯示它,並在編輯時按ctrl + k讓它爲你設置格式。 – Carcigenicate
我正在嘗試添加錯誤代碼的屏幕快照,但此評論框不允許我使用。 – Jkeef
另外,我不知道CTRL + K命令做了什麼,但是我的代碼中的其他紅線現在都消失了嗎? – Jkeef