2017-01-16 76 views
0

我的部分java代碼未運行。我對Java相當陌生,一直在研究一些新的環境變化。我的班級被告知要建立一個Windchill溫度計算器。我的主要問題是,我的代碼工作到for(ws = wsp; ws < = c; ws + = 0.5)然後停止。部分代碼未運行

import java.util.*; 
class Assign1 
{ 
    public static void main(String args[]) 
{ 
    Menu user = new Menu(); 
    Menu.mainmenu(); 
    user.acceptSelection(); 
} 
} 
class Menu 
{ 
    public static void mainmenu() 
{ 
    System.out.println("Temperature Analysis MENU"); 
    System.out.println("1.W)ind Chill Temperature"); 
    System.out.println("0.E)xit"); 
    System.out.println("Enter Selection:"); 
} 
public void acceptSelection() 
{ 
    Scanner stdin = new Scanner(System.in); 
    String selection = stdin.nextLine(); 
    char choice = selection.charAt(0); 

    switch(choice) 
    { 
    case 'W': 
    case 'w': 
    case '1': 
       processing.process(); break; 
    case 'E': 
    case 'e': 
    case '0': 
       System.out.println("E"); break; 
    } 
} 
} 
class processing 
{ 
public static void process() 
{ 
    Scanner stdin = new Scanner(System.in); 
    System.out.println("\n\n\n\n\n\n\n"); 
    System.out.print("Please enter START air temp in celsius (decimal) MUST be BELOW 9: "); 
    double sa = stdin.nextDouble(); 
    System.out.print("Please enter END air temp in celsius (decimal) MUST be BELOW 9: "); 
    double ea = stdin.nextDouble(); 
    System.out.print("Please enter wind speed (decimal) FROM 8km/h to: "); 
    double w = stdin.nextDouble(); 
    System.out.println("\n==================================================================\n"); 
    calculation(sa, ea, w); 

} 
public static void calculation(double a, double b, double c) 
{ 
    double wsp = 8.0; 
    double airTemp; 
    double ws; 
    int size = 150; 
    double[] wChill = new double[size]; 
    int count = 0; 
    System.out.print(" " + a); 
    while(a <= b) 
    { 
     System.out.print(" " + a); 
     a +=5; 
     count++; 
    } 
    System.out.print(" " + b); 
    int count2 = 0; 
    while(wsp <= c) 
    { 
     count2++; 
     wsp += 0.5; 
    } 
    double[][] chart = new double[count2][count]; 
    int i = 0, j = 0, k = 0; 

這是停止工作的地方。我無法將它打印出來。任何幫助解決我的問題將不勝感激,以及筆記我的代碼,因爲我想改善。如果有幫助,我正在使用JGrasp。

 for (ws = wsp; ws <= c; ws += 0.5) 
    { 
    System.out.println(ws + " "); 
    for (airTemp = a; airTemp <= b; airTemp += 5.0) 
    { 
     if ((ws + 0.5) > c) 
     { 
     System.out.printf("%2d %2d", c , chart[k][i]); 
     } 
     else 
     { 
     wChill[i] = (13.12 + (0.6215*airTemp)+(-11.37*Math.pow(ws, 0.16))+(0.3965*airTemp*Math.pow(ws, 0.16))); 
     chart[k][i] = wChill[i]; 
     System.out.print(chart[k][i] + " "); 
     } 
     i++; 
    } 
    k++; 
    } 


} 


} 
+3

它如何停止工作?崩潰?錯誤的值出來了?它進入一個無限循環?請添加更多關於出錯的信息,以及您對'a','b'和'c'的值(我強烈建議將其重命名爲'startTemp','endTemp'和'maxWindSpeed'等更有意義的名稱。並且Eclipse有一個內置的調試器;學會使用它,因爲在for(ws = wsp; ...)上放置一個斷點將提供很多信息。 –

+0

歡迎來到Stack Overflow!爲了給你一個很好的答案,它可能會幫助我們,如果你有一個[問],如果你還沒有看過它,它可能也是有用的,如果你可以提供[mcve] – Mat

回答

1

根據你的代碼,你有一個while循環

while(wsp <= c) {...} 

那麼你有一個for循環

for (ws = wsp; ws <= c; ws += 0.5) 

所以你可以看到ws指派其具有的wsp值在此期間已經超過了價值c

+0

非常感謝,我很感激。 –