我编写了一个简单的程序,以风速(km / h)为单位将其转换为Knots或Beaufort,并指出风是欧洲风暴或平静。起始速度需要输入主类,但我不能将其转移到第二类。此外,我很难从主要方法调用第二类方法。
我的主要课程:
import java.util.Scanner;
public class WindSpeedTester {
public static void main(String[] args){
System.out.println("Enter speed in km/hr.");
Scanner kb = new Scanner(System.in);
Double input = kb.nextDouble();
WindStorm windSpeed = new WindStorm();
System.out.println("What would you want wind speed in?");
System.out.println("Enter 1 for km/hr, 2 for Beaufort, 3 for Knots, or 4 to exit.");
int count = kb.nextInt();
if (count < 0){
while (count <0)
System.out.println("Invalid input . Please enter a valid integer.");
count = kb.nextInt();}
else{
while (count != 4 ){
if (count == 1){
windSpeed.getKilometerPerHour(kilometersPerHour);}
else if (count == 2){
windSpeed.getBeaufort(kilometersPerHour);}
else if (count == 3){
windSpeed.getKnots(kilometersPerHour);}
else if (count == 4){
System.out.println("Bye.");
System.exit(0);}
else{
System.out.print("Invalid input.");}
}
}
windSpeed.isCalm(kilometersPerHour);
windSpeed.isOrcan(kilometersPerHour);
kb.close();
}
}
WindStorm
上课:
import java.util.Scanner;
public class WindStorm {
/**
* the wind speed in kilometer per hour
*/
private double kilometerPerHour = 0.0;
public double windSpeed(double kilometerPerHour){
double windSpeed = kilometerPerHour + input; //input is in main
Scanner kb = new Scanner(System.in);
while (windSpeed < 0){
System.out.println("The wind speed entered is invalid. Enter another speed.");
windSpeed = kb.nextDouble();}
kb.close();
return windSpeed;
}
/**
* Return true if this wind speed is a
* European windstorm (Orcan, more than
* 120 km/h).
*/
public boolean isOrcan(double kilometersPerHour) {
boolean isOrcan = true;
if (kilometersPerHour > 120)
System.out.println("The wind speed is Orcan.");
return isOrcan;
}
/**
* Return true if this wind speed is
* calm (less than 2 km/h).
*/
public boolean isCalm(double kilometersPerHour) {
boolean isCalm = true;
if (kilometersPerHour < 2)
System.out.println("The wind is calm.");
return isCalm;
}
public double getKilometerPerHour(double kilometersPerHour) {
double getKilometerPerHour = kilometersPerHour;
System.out.println("The wind speed is: " + kilometersPerHour + " km/h.");
return getKilometerPerHour;
}
/**
* Returns the wind speed measured in knots (nautic miles).
* A nautic mile is 1.852 kilometer.
*/
public double getKnots(double kilometersPerHour) {
double getKnots = kilometersPerHour / 1852;
System.out.println("The wind speed is: " + getKnots + "Knots (nautical miles).");
return getKnots;
}
/**
* Returns the wind speed measured in Beaufort (int 1-12).
*/
public int getBeaufort(double kilometersPerHour) {
int getBeaufort = (int) Math.pow((kilometersPerHour / 3.01), (2/3));
getBeaufort = (int) (getBeaufort + 0.5);
if (getBeaufort > 12)
System.out.println("Invalid measurement.");
else if (getBeaufort <= 0)
System.out.println("Invalid measurement.");
else
System.out.println("The wind speed is: " + getBeaufort + "Knots.");
return getBeaufort;
}
}
运行此代码,我收到以下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at WindStorm.kilometersPerHour(WindStorm.java:15)
at WindStorm.isOrcan(WindStorm.java:29)
at WindSpeedTester.main(WindSpeedTester.java:26)