0

我希望能够读取输入星并计算方法中的距离并在主中调用答案。我该怎么做呢?这是我到目前为止所拥有的。它的作用是找到我需要显示的 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;
                        }
                   }
4

2 回答 2

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);

        }
 }
于 2012-05-04T16:46:11.173 回答
0

您的主要方法应该看起来像这样:

public static void main (String [] args) {
    System.out.println(findDistance());
}

这将要求您将 findDistance 的方法标头更改为:

public static double findDistance ()  { ...

(因为您从不使用该string distance参数)。

于 2012-05-04T16:13:17.953 回答