【问题标题】:have trouble with calling methods. what am i missing?调用方法有问题。我错过了什么?
【发布时间】:2014-10-03 15:15:18
【问题描述】:

请修复我。我收到多条错误消息 “变量 airSpeed_km 可能尚未初始化” “可变宽度可能尚未初始化” “可变长度可能尚未初始化”

import java.util.Scanner;
  public class V4_________1{

 public static void main (String args[])
 {
   Scanner keyboard = new Scanner(System.in);
   double KNOTS_TO_KMPHR;
   double airSpeed_km;
   double airSpeed_knots;
   double width;
   double length;
                            ***// need to do something in the main but not sure what exactly***
   airSpeed_knots = keyboard.nextDouble();
   System.out.println("what is your current airspeed in knots?");
   System.out.println("your current airspeed in km is: " + airSpeed_km  + "your holding pattern             width is: " + width + "your holding patter length is: " + length);
 }  
    public static double getAirSpeed(double airSpeed_knots, double KNOTS_TO_KMPHR, double    airSpeed_km)
{ 
  KNOTS_TO_KMPHR = 1.852;
  airSpeed_km = airSpeed_knots * KNOTS_TO_KMPHR ;
  return airSpeed_km;
}

public static double calcPatternWidth(double width, double airSpeed_km)
{ 
  width = (airSpeed_km) / (60 * Math.PI) * 2;  
  return width;
}  

public static double calcPatternLength(double airSpeed_km, double length)
{
  length = (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
  return length;
} 



}   

【问题讨论】:

标签: java methods calling-convention


【解决方案1】:

您声明:

   double airSpeed_km;

使用后:

   System.out.println("your current airspeed in km is: " + airSpeed_km  + "your holding pattern             width is: " + width + "your holding patter length is: " + length);

没有任何分配。所以你得到一个错误,你可以通过给它一个默认值 0 来防止这种情况发生。

 double airSpeed_km = 0;

(您的其他错误也是如此)

【讨论】:

    【解决方案2】:

    在 Java 中,如果一个变量甚至可以在没有值的情况下使用,编译器就会感到不安。 因此,最好在首次声明变量时始终为变量赋值。 由于您通常不知道变量在声明时的值,因此通常的做法是给它一个零值。

    所以你的声明应该是这样的:

    double KNOTS_TO_KMPHR=0;
    double airSpeed_km=0;
    double airSpeed_knots=0;
    double width=0;
    double length=0;
    

    这将处理您所有的“[] 可能未初始化”编译器错误。

    【讨论】:

      【解决方案3】:

      您的代码是正确的。当您将它们传递给函数时,您无法设置变量。查看这两种方法并了解发生了什么。 你可以这样做:

       public static void main (String args[])
       {
         Scanner keyboard = new Scanner(System.in);
         double KNOTS_TO_KMPHR=1.852;
         double airSpeed_knots;
         System.out.println("what is your current airspeed in knots?");
         airSpeed_knots = keyboard.nextDouble();
      
         System.out.println("your current airspeed in km is: " + getAirSpeed(airSpeed_knots)  + "your holding pattern             width is: " + calcPatternWidth(getAirSpeed(airSpeed_knots)) + "your holding patter length is: " + calcPatternLength(getAirSpeed(airSpeed_knots));
       }  
       public static double getAirSpeed(double airSpeed_knots)
      { 
          return airSpeed_knots * KNOTS_TO_KMPHR ;
      }
      
      public static double calcPatternWidth(double airSpeed_km)
      { 
        return (airSpeed_km) / (60 * Math.PI) * 2;  
      }  
      
      public static double calcPatternLength(double airSpeed_km)
      {
        return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
      } 
      

      如果你想设置变量,也可以这样做:

       public static void main (String args[])
       {
         Scanner keyboard = new Scanner(System.in);
         double KNOTS_TO_KMPHR=1.852;
         double airSpeed_knots;
         System.out.println("what is your current airspeed in knots?");
         airSpeed_knots = keyboard.nextDouble();
      
         double airSpeed_km=getAirSpeed(airSpeed_knots);
         double width=calcPatternWidth(airSpeed_km);
         double length= calcPatternLength(airSpeed_km);
      
         System.out.println("your current airspeed in km is: " + airSpeed_km  + "your holding pattern             width is: " + width + "your holding patter length is: " + length);
       }  
       public static double getAirSpeed(double airSpeed_knots)
      { 
          return airSpeed_knots * KNOTS_TO_KMPHR ;
      }
      
      public static double calcPatternWidth(double airSpeed_km)
      { 
        return (airSpeed_km) / (60 * Math.PI) * 2;  
      }  
      
      public static double calcPatternLength(double airSpeed_km)
      {
        return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        • 2022-07-06
        • 2014-01-14
        相关资源
        最近更新 更多