2017-09-17 296 views
0

我正在使用公式,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); 

    } 
} 

任何有關這個問題的幫助將不勝感激。

+1

請顯示確切的錯誤,並附有堆棧跟蹤。另外,請嘗試格式化您的代碼。突出顯示它,並在編輯時按ctrl + k讓它爲你設置格式。 – Carcigenicate

+0

我正在嘗試添加錯誤代碼的屏幕快照,但此評論框不允許我使用。 – Jkeef

+0

另外,我不知道CTRL + K命令做了什麼,但是我的代碼中的其他紅線現在都消失了嗎? – Jkeef

回答

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返回類型)。因此,您不能將此類調用的值分配給變量。

+0

因此,使用這種方法會產生相同的結果,就像我將它設置爲變量(減去錯誤代碼)一樣,在所有其他操作完成後它仍會最後執行? – Jkeef

+0

@Jkeef:它會在所有其他操作之後完成,因爲它在所有其他操作之後出現。 Java中的函數/方法從上到下運行(服從那裏存在的任何流控制結構)。該行的'resultMessage ='部分與執行代碼行時沒有關係。 –

相關問題