在我的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
最佳答案
您没有正确初始化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
的参数。我想这就是您想要实现的目标。完成后,您实际上应该关闭Scanner对象,以便在返回之前将
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