在我的代码double getAirSpeed
double calcPatternwidth
和double calcPatternLength
未正确初始化,为什么?
/**
* holding patterns
* assignment 1
* question 2
**/
import java.util.Scanner;
public class StockiColeA1Q2 {
public static void main(String []args) {
Scanner keyboard = new Scanner(System.in);
double getAirSpeed ;
double calcPatternWidth;
double calcPatternLength;
System.out.println("That speed is " + getAirSpeed +
"\nHolding pattern width: " + calcPatternWidth +
"kms\nHolding pattern length: " + calcPatternLength + "kms");
}//main
public static double getAirSpeed() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the speed in Knots: ");
double knots = keyboard.nextDouble(); //knots given by user
return knots*1.852;
}//get air speed
public static double calcPatternWidth(double knots) {
return (knots/60*Math.PI)*2;
}//patern width
public static double calcPatternLength(double knots) {
return knots/60 + (knots/60*Math.PI)*2;
}//pattern width
}//class
答案 0 :(得分:2)
您没有正确初始化main中的变量。您显示调用函数并预先分配它们然后显示它们。我认为你所寻找的是main
看起来很喜欢这个:
public static void main(String[] args) {
double getAirSpeed = getAirSpeed();
double calcPatternWidth = calcPatternWidth(getAirSpeed);
double calcPatternLength = calcPatternLength(getAirSpeed);
System.out.println("That speed is " + getAirSpeed + "\nHolding pattern width: " + calcPatternWidth
+ "kms\nHolding pattern length: " + calcPatternLength + "kms");
}// main
上面的代码使用getAirSpeed
作为calcPatternWidth
和calcPatternLength
的参数。我猜这就是你想要完成的事情。
完成后你真的应该关闭扫描仪对象,所以我会在返回之前修改getAirSpeed()
来调用keyboard.close()
。
public static double getAirSpeed() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the speed in Knots: ");
double knots = keyboard.nextDouble(); // knots given by user
keyboard.close();
return knots * 1.852;
}// get air speed
答案 1 :(得分:0)
在这里,我无法看到您在哪里初始化给定的变量。由于所有三个vairables都是main方法的本地,因此编译器不会为它们分配默认值。只是创建那些变量的getter将不会赋值。如果你在类中使用main方法创建变量,编译器将分配defaut值,但你必须使它们为静态,因为main方法是静态的,你不能在静态块中使用非静态变量。