我想能够读取输入星并计算方法中的距离,并在主要中调用答案。我该怎么做呢?这是我到目前为止所拥有的。它的作用是找到我需要显示的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;
}
}
答案 0 :(得分:0)
你的主要方法可能应该是这样的:
public static void main (String [] args) {
System.out.println(findDistance());
}
这需要您将findDistance的方法标题更改为:
public static double findDistance () { ...
(因为你从不使用string distance
参数)。
答案 1 :(得分:0)
我更改了您的方法签名,因为您没有使用传入的变量并正确地重命名了Distance类。我还使findDistance方法不是静态的,其余方法不应该是静态方法,因为您可以创建类Java Classes的实例。你还有一些未使用的原始类型。
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Distance {
public double findDistance () {
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;
}
public static void main(String[] args){
Distance d = new Distance();
d.findDistance(null);
}
}