Java クラスのアプリケーションをデバッグしていて、ケース 39 で intPos を 63 ではなく 62 に変更すると、switch ステートメントが機能しなくなりました。すべてのケースでコンソール出力を出力する代わりに、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
最後の高度だけでなく、すべての高度の気象出力を出力するようにコードを機能させるにはどうすればよいですか?
編集: 最後のケースが 2 ではなく 3 の場合に機能する理由は、case39 が実行される唯一のケースであるすべての状況で最後のケースをコンパイルしないためです。