【发布时间】:2013-03-10 06:56:32
【问题描述】:
我正在调试一个 java 类的应用程序,当我在 case 39 中更改为使 intPos 62 而不是 63 不再起作用时,我的 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℃ . . .
而不是像我在案例 39 错误获得时那样获取所有高度的信息
intPost = 63
如何使我的代码正常工作,以便打印出所有高度的天气输出,而不仅仅是最后一个?
编辑:只是想出了一些更多信息,当最后一个案例是 3 而不是 2 时它起作用的原因是因为它不会在所有情况下编译最后一个案例,当 case39 被执行时,它是唯一运行的案例
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖SSCCE。
-
好的,我在发布问题之前遇到了问题,当时我将问题缩短并遗漏了代码的关键部分,所以我将所有内容都放在这里,哪些部分过多且不需要?
-
你的 switch 语句中的“默认”在哪里?
-
我在语句中没有默认值,因为我只传递其中定义的值我尝试添加一个默认值:System.out.println("error");休息;并且没有任何改变我也不确定如何在 cmets 中格式化代码,因为 enter 提交评论并且不会换行
-
首先,cmets 并不是用来存放大量代码的。编辑您的主要问题以将新代码放入其中。其次,我认为您的 switch 语句没有任何问题。会不会是输入错误的值?
标签: java switch-statement