2012-05-04 5 views
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; 
         } 
        } 
+0

コード内で動作しないことについて具体的に説明できますか? – assylias

+1

あなたの 'main'メソッドが行う唯一のことは、あなたが投稿したコードから抜けている' place() 'を呼び出すことです。コードの残りの部分は、決して呼び出されないように見えるので、無意味に見えます。あなたの質問を編集して、私たちが答えるかもしれない質問を提示してください。 –

答えて

0

あなたの主な方法は、おそらく次のようになります。

あなたがするfindDistanceのためのあなたの方法ヘッダーを変更する必要になる
public static void main (String [] args) { 
    System.out.println(findDistance()); 
} 

:あなたが使用することはありませんので

public static double findDistance() { ... 

string distanceパラメータ)。

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

     } 
} 
関連する問題