0

我正在学习一个 Java 类,并有以下示例来帮助我使我的代码运行良好,但我无法让我的代码运行。我发现问题出在 strAlt 上,但是我不知道 strAlt 在以下代码中的定义位置。在 Net beans 中使用调试器时,我发现当它第一次在 switch 测试中使用时,strAlt 被定义为字符串“03”,它被转换为整数 3,但我不确定字符串 03 的来源。

public class NWSFB
{
   /** A class variable containing the Station Weather */
   private String strWea ;
   /** The Constructor */
   public NWSFB(String strVar)
   {
      // this is the constructor
       strWea = strVar;
   }


public String getWindInfo(String strAlt)
{
   String strRet;
   strRet = "The wind direction for " + strAlt + "000 feet is " + getWindDir(strAlt);
   return strRet;
}
/**
This routine will accept a string containing the altitude
and will return the starting position of the altitude weather
as an integer.
@param strAlt A string containing the altitude
@return An integer showing the position of the altitude weather within the station weather              
*/
private int getPos(String strAlt)
{
   int intAlt;
   int intRet =0;
   intAlt = Integer.parseInt(strAlt);
   switch (intAlt)
   {
     case 3:
      intRet = 4;
      break;
     case 6:
      intRet = 9;
      break;
     case 9:
      intRet = 14;
     // etc .... you can figure out the the other altitudes
   }
   return intRet;
}
 public String getAltitudeWeather(String strAlt)
 {
   // get the position in the station weather string
   int intPos = getPos(strAlt);

   // strAltitudeWeather contains a seven character string 
   String strRet = strWea.substring(intPos,intPos+7);

   // return the result
   return strRet;
 }
public String getWindDir(String strAlt)
 {
   String strRet = getAltitudeWeather(strAlt);
   return strRet.substring(0,2);
 }
}

此类用于按如下方式运行类天气

// this code will be saved in a file called Weather.java
public class weather
{
   public static void main(String[] args)
    {
      // the next line is very long and ends with 352853
        //This code doesn't really end in 352853 I think when it did it was getting   diffrent weather data
     final String FAA_FD="SAN 3106 2915+23 2714+16 0710+08 1010-07 1916-17 222331 213141 203753";
     System.out.println("Start of Program Weather") ;
     NWSFB windsAloft = new NWSFB(FAA_FD);
     System.out.println(windsAloft.getWindInfo("03"));
     System.out.println(windsAloft.getWindInfo("06"));
     System.out.println(windsAloft.getWindInfo("09"));
     System.out.println(windsAloft.getWindInfo("12"));
     // etc. for the other altitudes
     System.out.println("End of Program Weather") ;
  }
}
4

1 回答 1

2

这就是字符串的来源。

System.out.println(windsAloft.getWindInfo("03"));
System.out.println(windsAloft.getWindInfo("06"));
System.out.println(windsAloft.getWindInfo("09"));
System.out.println(windsAloft.getWindInfo("12"));

字符串NWSFB通过getWindInfo(). 从那里它被传递到getWindDir() TogetAltitudeWeather() 然后最后到getPos()

于 2013-03-10T05:35:31.557 回答