调用方法有问题。我错过了什么?

时间:2014-10-03 15:15:19

标签: java methods calling-convention

修复我的问题。我收到多条错误消息 “变量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;
} 



}   

3 个答案:

答案 0 :(得分: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;

(同样适合您的其他错误)

答案 1 :(得分:0)

在Java中,如果在没有值的情况下使用变量甚至MIGHT,编译器会感到沮丧。 因此,最好在首次声明变量时始终为变量赋值。 由于您通常不知道变量在声明时将具有的值,因此通常会将其赋值为零。

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

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

这会照顾你所有的“[]可能没有被初始化”编译器错误。

答案 2 :(得分:0)

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

 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);
}