我希望能够读取输入星并计算方法中的距离并在主中调用答案。我该怎么做呢?这是我到目前为止所拥有的。它的作用是找到我需要显示的 5 颗星的距离。
谢谢!!
package desktop;
import java.util.Scanner;
import javax.swing.JOptionPane;
// *import io because of the file writing
public class distance {
public static void main (String [] args) {
double d = place ();
}
public static double findDistance (String distance) {
double result;
Scanner sc = new Scanner(System.in);
System.out.println("Where would you like to go?");
System.out.println();
System.out.println("Enter 1 for Proxima Centauri");
System.out.println("Enter 2 for Bernard's Star");
System.out.println("Enter 3 for Sirius A");
System.out.println("Enter 4 for Epsilon Eridani");
System.out.println("Enter 5 for Betelgeuse");
System.out.println();
System.out.println();
double operator;
int place = sc.nextInt();
switch (place) {
case 1:
result = timePC ();
System.out.println();
System.out.println();
System.out.println("Time to Proxima Centauri is: " + String.format("%.4g",timePC()) + " lightyears");
break;
case 2:
result = timeBS ();
System.out.println();
System.out.println();
System.out.println("Time to Bernand's Star is: " + String.format("%.4g",timeBS()) + " lightyears");
break;
case 3:
result = timeSA ();
System.out.println();
System.out.println();
System.out.println("Time to Sirius A is: " + String.format("%.4g",timeSA()) + " lightyears");
break;
case 4:
result = timeEE ();
System.out.println();
System.out.println();
System.out.println("Time to Epsilon Eridani is: " + String.format("%.4g",timeEE()) + " lightyears");
break;
case 5:
result = timeB ();
System.out.println();
System.out.println();
System.out.println("Time to Betelgeuse is:" String.format("%.4g",timeB()) + " lightyears" );
break;
default:
System.out.println("Invalid function");
}
return place;
}
public static double timePC () {
double result;
double CC = 3.16887*Math.pow(10,7);
double distance = 4.010*Math.pow(10, 16);
double velocity = 3*Math.pow(10, 8);
result = (distance / velocity)/CC;
return result;
}
public static double timeBS () {
double result;
double CC = 3.16887*Math.pow(10,7);
double distance = 5.637*Math.pow(10, 16);
double velocity = 3*Math.pow(10, 8);
result = (distance / velocity)/CC;
return result;
}
public static double timeSA () {
double result;
double CC = 3.16887*Math.pow(10,7);
double distance = 3.592*Math.pow(10, 18);
double velocity = 3*Math.pow(10, 8);
result = (distance / velocity)/CC;
return result;
}
public static double timeEE () {
double result;
double CC = 3.16887*Math.pow(10,7);
double distance = 2.930*Math.pow(10, 18);
double velocity = 3*Math.pow(10, 8);
result = (distance / velocity)/CC;
return result;
}
public static double timeB () {
double result;
double CC = 3.16887*Math.pow(10,7);
double distance = 6.079*Math.pow(10, 18);
double velocity = 3*Math.pow(10, 8);
result = (distance / velocity)/CC;
return result;
}
}