我正在学习Java类,并具有以下示例来帮助我使我的代码正常运行,但无法运行我的代码。我发现问题出在strAlt上,但是我不能说出以下代码在哪里定义了strAlt。在Net Bean中使用调试器时,我发现在开关测试中首次使用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") ;
  }
}

最佳答案

这是字符串的来源。

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()
getAltitudeWeather()
然后最后到getPos()

10-04 18:14