繁体   English   中英

切换仅执行一条语句

[英]Switch only executing one statement

我正在调试Java类的应用程序,并在将情况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 C。

而不是像案例39错误地那样获取所有海拔高度的信息

intPost =  63

如何使代码正常工作,以便打印出所有高度的天气输出,而不仅仅是最后一个?

编辑:只是弄清楚了一些更多的信息,当最后一种情况是3而不是2时,它起作用的原因是因为当case39被执行时,它不会在所有情况下都编译最后一种情况,这是唯一可以运行的情况

我看不到问题-只是将代码复制到文件中,进行编译并运行...结果在最后,我得到了所有海拔高度的结果!

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
. . .
*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM