2014-10-03 60 views
-2

修理我PLZ。我得到多個錯誤消息 「變量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; 
} 



} 
+0

的可能重複[變量可能尚未初始化錯誤](http://stackoverflow.com/問題/ 2448843 /可變可能-不具備的,被初始化錯誤) – Boann 2014-10-14 16:04:57

回答

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; 

(同樣適用於你的其他錯誤)

0

在Java中,編譯器得到不高興,如果一個變量,甚至可能沒有值被使用。 因此最好的做法是在首次聲明變量時始終給變量賦值。 由於您通常不知道變量在聲明時的值,因此通常將其值設爲零。

所以你的聲明應該是這樣的:

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

這將帶您所有的「[]可能尚未初始化」編譯器錯誤的照顧。

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