【问题标题】:how to read the input star and calculate the distance in a method and the call the answer in the main?如何读取输入星并计算方法中的距离并在主中调用答案?
【发布时间】:2012-05-14 04:58:00
【问题描述】:

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

【问题讨论】:

  • 您能否具体说明您的代码中哪些地方不起作用?
  • 您的main 方法所做的唯一事情是调用place(),您发布的代码中缺少此功能。所有其余的代码似乎毫无意义,因为它似乎永远不会被调用。请编辑您的问题,以便它提出一个我们可能能够回答的问题。

标签: java methods call java.util.scanner main


【解决方案1】:

你的 main 方法应该是这样的:

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

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

public static double findDistance ()  { ...

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

【讨论】:

    【解决方案2】:

    我更改了您的方法签名,因为您没有使用传入的变量并正确重命名了 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);
    
            }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      相关资源
      最近更新 更多