2013-03-10 100 views
0

我正在調試一個java類的應用程序,並且在情況39中更改了switch語句以使intPos 62而不是63不再起作用。現在不用在所有情況下打印控制檯輸出,而只是爲switch語句中的最後一種情況提供它。具有switch語句的類的代碼如下。切換隻執行一條語句

public class NWSFB 
{ 
    /** A class variable containing the Station Weather */ 
    private String strWeather ; 
    /** The Constructor */ 
    public NWSFB(String strVar) 
    { 
     strWeather = strVar; 
    } 
    /** A method that finds the station */  
    public String getStationID(String strVar) 
    { 
    String stationId = strVar ; 
    return stationId.substring(0,3); 
    }  
public String getWindInfo(String strAlt) 
{ 
    String strRet; 
    strRet = "The altitude weather for " + strAlt + "000 feet is " +  getAltitudeWeather(strAlt) 
     + "\nWind Direction:" +getWindDir(strAlt) +"0 degrees" 
     + "\nWind Speed:" +getWindSpeed(strAlt) + " knots" 
     + "\nWind temperature:" +getWindTemperature(strAlt) + "C" 
     + "\n. . ." 
     + "\n"; 


    return strRet; 
} 

private int getPos(String strAlt) 
{ 
    int intAlt; 
    int intPos =0; 
    intAlt = Integer.parseInt(strAlt); 
    switch (intAlt) 
    { 
    case 3: 
     intPos = 4; 
     break; 
    case 6: 
     intPos = 9; 
     break; 
    case 9: 
     intPos = 17; 
     break; 
    case 12: 
     intPos = 25; 
     break; 
    case 18: 
     intPos = 33; 
     break; 
    case 24: 
     intPos = 41; 
     break; 
    case 30: 
     intPos = 49; 
     break; 
    case 34: 
     intPos = 56; 
     break; 
    case 39: 
     intPos = 62; 
     break; 
    } 
    return intPos; 
} 
public String getAltitudeWeather (String strAlt) 
{ 
    int intPosition = getPos(strAlt) ; 
    String strPos = strWeather.substring(intPosition, intPosition+7); 
    return strPos ; 
} 
//get wind direction 
public String getWindDir(String strAlt) 
{ 
    String strPos = getAltitudeWeather(strAlt); 
    return strPos.substring(0,2); 

} 
//get wind speed 
public String getWindSpeed(String strAlt) 
{ 
    String strPos = getAltitudeWeather(strAlt); 
    return strPos.substring(2,4); 
} 
//get wind temperature 
public String getWindTemperature(String strAlt) 
{ 
    String strPos = getAltitudeWeather(strAlt); 
    return strPos.substring(4,7); 
} 


} 

這是使用類的代碼

public class A19005 
{ 

    static String strStationWeather = "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652" ; 
    public static void main(String[] args) 
    { 
    //Create the myWeather object 
     NWSFB myWeather = new NWSFB(strStationWeather); 
    //use myWeather to get the weather at various altitudes 
     System.out.println("Sation ID: " + myWeather.getStationID(strStationWeather)); 
     System.out.println(myWeather.getWindInfo("03")); 
     System.out.println(myWeather.getWindInfo("06")); 
     System.out.println(myWeather.getWindInfo("09")); 
     System.out.println(myWeather.getWindInfo("12")); 
     System.out.println(myWeather.getWindInfo("18")); 
     System.out.println(myWeather.getWindInfo("24")); 
     System.out.println(myWeather.getWindInfo("30")); 
     System.out.println(myWeather.getWindInfo("34")); 
     System.out.println(myWeather.getWindInfo("39")); 
    } 
} 

當我運行該程序,我得到 海拔天氣39000英尺是192652 風向:210度 風速:19節 風溫:41 C 。 。 。

而是得到這個信息對於所有海拔像我一樣當殼39不正確有

intPost = 63 

如何讓我的代碼工作,所以它打印出的所有高度,而不是僅在過去的一個天氣輸出?

編輯:只是想出了一些更多的信息,當最後一種情況是3而不是2時,它的工作原理是因爲它不編譯所有情況下的最後一種情況,當case39執行時它唯一的情況下運行

+1

爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-03-10 06:57:35

+0

好的我在發佈一個問題之前遇到了問題,當我縮短了時間並且忽略了代碼的一個關鍵部分,所以我只是把這裏的一切放在了哪裏,哪部分是過度的而不是必需的? – 2013-03-10 07:03:00

+0

switch語句中的「default」在哪裏? – 2013-03-10 07:03:59

回答

0

我沒有看到問題 - 只是將代碼複製到一個文件中,編譯並運行它......結果在最後,我得到了所有海拔高度的結果!

public class P4 
{ 
    static String strStationWeather = "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652" ; 

    public static void main(String[] args) 
    { 
    //Create the myWeather object 
     NWSFB myWeather = new NWSFB(strStationWeather); 
    //use myWeather to get the weather at various altitudes 
     System.out.println("Sation ID: " + myWeather.getStationID(strStationWeather)); 
     System.out.println(myWeather.getWindInfo("03")); 
     System.out.println(myWeather.getWindInfo("06")); 
     System.out.println(myWeather.getWindInfo("09")); 
     System.out.println(myWeather.getWindInfo("12")); 
     System.out.println(myWeather.getWindInfo("18")); 
     System.out.println(myWeather.getWindInfo("24")); 
     System.out.println(myWeather.getWindInfo("30")); 
     System.out.println(myWeather.getWindInfo("34")); 
     System.out.println(myWeather.getWindInfo("39")); 
    } 
} 

class NWSFB 
{ 
    /** A class variable containing the Station Weather */ 
    private String strWeather ; 

    /** The Constructor */ 
    public NWSFB(String strVar) 
    { 
     strWeather = strVar; 
    } 

    /** A method that finds the station */  

    public String getStationID(String strVar) 
    { 
    String stationId = strVar ; 
    return stationId.substring(0,3); 
    }  

public String getWindInfo(String strAlt) 
{ 
    String strRet; 
    strRet = "The altitude weather for " + strAlt + "000 feet is " +  getAltitudeWeather(strAlt) 
     + "\nWind Direction:" +getWindDir(strAlt) +"0 degrees" 
     + "\nWind Speed:" +getWindSpeed(strAlt) + " knots" 
     + "\nWind temperature:" +getWindTemperature(strAlt) + "C" 
     + "\n. . ." 
     + "\n"; 

    return strRet; 
} 

private int getPos(String strAlt) 
{ 
    int intAlt; 
    int intPos =0; 
    intAlt = Integer.parseInt(strAlt); 
    switch (intAlt) 
    { 
    case 3: 
     intPos = 4; 
     break; 
    case 6: 
     intPos = 9; 
     break; 
    case 9: 
     intPos = 17; 
     break; 
    case 12: 
     intPos = 25; 
     break; 
    case 18: 
     intPos = 33; 
     break; 
    case 24: 
     intPos = 41; 
     break; 
    case 30: 
     intPos = 49; 
     break; 
    case 34: 
     intPos = 56; 
     break; 
    case 39: 
     intPos = 62; 
     break; 
    } 

    return intPos; 
} 

public String getAltitudeWeather (String strAlt) 
{ 
    int intPosition = getPos(strAlt) ; 
    String strPos = strWeather.substring(intPosition, intPosition+7); 
    return strPos ; 
} 

//get wind direction 
public String getWindDir(String strAlt) 
{ 
    String strPos = getAltitudeWeather(strAlt); 
    return strPos.substring(0,2); 
} 

//get wind speed 
public String getWindSpeed(String strAlt) 
{ 
    String strPos = getAltitudeWeather(strAlt); 
    return strPos.substring(2,4); 
} 

//get wind temperature 
public String getWindTemperature(String strAlt) 
{ 
    String strPos = getAltitudeWeather(strAlt); 
    return strPos.substring(4,7); 
} 
} 

/* Output: 
Sation ID: SAN 

The altitude weather for 03000 feet is 1905 18 
Wind Direction:190 degrees 
Wind Speed:05 knots 
Wind temperature: 18C 
. . . 


The altitude weather for 06000 feet is 1808+24 
Wind Direction:180 degrees 
Wind Speed:08 knots 
Wind temperature:+24C 
. . . 


The altitude weather for 09000 feet is 1512+17 
Wind Direction:150 degrees 
Wind Speed:12 knots 
Wind temperature:+17C 
. . . 


The altitude weather for 12000 feet is 1209+10 
Wind Direction:120 degrees 
Wind Speed:09 knots 
Wind temperature:+10C 
. . . 


The altitude weather for 18000 feet is 1708-06 
Wind Direction:170 degrees 
Wind Speed:08 knots 
Wind temperature:-06C 
. . . 


The altitude weather for 24000 feet is 2016-16 
Wind Direction:200 degrees 
Wind Speed:16 knots 
Wind temperature:-16C 
. . . 


The altitude weather for 30000 feet is 211831 
Wind Direction:210 degrees 
Wind Speed:18 knots 
Wind temperature:31 C 
. . . 


The altitude weather for 34000 feet is 211941 
Wind Direction:210 degrees 
Wind Speed:19 knots 
Wind temperature:41 C 
. . . 


The altitude weather for 39000 feet is 192652 
Wind Direction: 10 degrees 
Wind Speed:92 knots 
Wind temperature:652C 
. . . 
*/ 
+0

我剛剛將這段代碼複製並粘貼到我的IDE中,但仍然只能得到最後一個高度的結果,你知道爲什麼會發生這種情況嗎? – 2013-03-10 07:34:26

+1

@AlexChapp - 您的IDE有點困惑。關閉你的IDE。現在,從命令行,使用'javac'重新編譯所有文件並使用'java'運行它。 – 2013-03-10 07:55:08

+0

從頭開始創建一個新項目粘貼我的代碼,它現在可以正常工作,所以你的權利讓我的IDE感到困惑 – 2013-03-10 08:03:54