优化一个简单的程序

时间:2015-08-18 16:52:08

标签: java formula

所以我已经编程了一段时间,而且我对程序的重要性感到困惑。格式良好"我写了这个简单的公式程序。怎么可能"更好"写的?在程序中,您可以为爱因斯坦时间膨胀方程输入一些数字,并输出在一定时间后扩张了多少时间。

 import java.util.Scanner;
 import java.lang.Math;

public class Yass {
Scanner sc = new Scanner(System.in);
static double velocity;
static double time;
static double dilated_time;
static double dilated_time_Minutes;
static double dilated_time_Hours;
static double dilated_time_Days;
static double dilated_time_Years;
static boolean timeIsSeconds = false;
static boolean timeIsMinutes = false;
static boolean timeIsHours = false;
static boolean timeIsDays = false;
static boolean timeIsYears = false;
public static final double c = 299792.458; // km/s


//Time in seconds, velocity in kilometers/second

public Yass(){

    getInput();
    executeEquation(time, velocity, c);

}


public void executeEquation(double t, double v, double c) {

    dilated_time = t / (Math.sqrt(1 - (Math.pow(v, 2) / Math.pow(c, 2))));

    if(dilated_time <= 60){
        timeIsSeconds = true;
    }
    if(dilated_time > 60 && dilated_time < (60*60)){
        dilated_time_Minutes = dilated_time / 60;
        timeIsMinutes = true;
    }
    if(dilated_time > (60*60) && dilated_time < (60*60)*24){
        dilated_time_Hours = (dilated_time / 60) / 60;
        timeIsHours = true;
    }
    if(dilated_time > (60*60)*24 && dilated_time < ((60*60)*24)*365){
        dilated_time_Days = ((dilated_time / 60) / 60) / 24;
        timeIsDays = true;
    }
    if(dilated_time > ((60*60)*24)*365){
        dilated_time_Years = (((dilated_time / 60) / 60) / 24) / 365;
        timeIsYears = true;
    }
}


public void getInput(){
    System.out.println("Enter the time you want to measure(stationary) in seconds: ");
    time = sc.nextDouble();
    System.out.println("\n Enter velocity in km/s: ");
    velocity = sc.nextDouble();
}


public static void main(String[] args){

    new Yass();

    if(timeIsSeconds){
        System.out.println("So if you're in a spaceship and travel at " + velocity + 
                " km/s. And start a clock who'll measure " + time + " seconds.\nBut at the same time a stationary spaceship start an exact same clock"
                        + " at the same time and measure\nthe same amount of time. After the moving spaceship have measured " + time + " seconds. Then "
                                + "the stationary\nspaceship will have measured " + dilated_time + " seconds. And therefore time has been running slower for the moving spaceship.");
    }
    if(timeIsMinutes){
        System.out.println("So if you're in a spaceship and travel at " + velocity + 
                " km/s. And start a clock who'll measure " + time + " seconds.\nBut at the same time a stationary spaceship start an exact same clock"
                        + " at the same time and measure\nthe same amount of time. After the moving spaceship have measured " + time + " seconds. Then "
                                + "the stationary\nspaceship will have measured " + dilated_time_Minutes + " minutes. And therefore time has been running slower for the moving spaceship.");
    }
    if(timeIsHours){
        System.out.println("So if you're in a spaceship and travel at " + velocity + 
                " km/s. And start a clock who'll measure " + time + " seconds.\nBut at the same time a stationary spaceship start an exact same clock"
                        + " at the same time and measure\nthe same amount of time. After the moving spaceship have measured " + time + " seconds. Then "
                                + "the stationary\nspaceship will have measured " + dilated_time_Hours + " hours. And therefore time has been running slower for the moving spaceship.");
    }
    if(timeIsDays){
        System.out.println("So if you're in a spaceship and travel at " + velocity + 
                " km/s. And start a clock who'll measure " + time + " seconds.\nBut at the same time a stationary spaceship start an exact same clock"
                        + " at the same time and measure\nthe same amount of time. After the moving spaceship have measured " + time + " seconds. Then "
                                + "the stationary\nspaceship will have measured " + dilated_time_Days + " days. And therefore time has been running slower for the moving spaceship.");
    }
    if(timeIsYears){
        System.out.println("So if you're in a spaceship and travel at " + velocity + 
                " km/s. And start a clock who'll measure " + time + " seconds.\nBut at the same time a stationary spaceship start an exact same clock"
                        + " at the same time and measure\nthe same amount of time. After the moving spaceship have measured " + time + " seconds. Then "
                                + "the stationary\nspaceship will have measured " + dilated_time_Years + " years. And therefore time has been running slower for the moving spaceship.");
    }
}
}

示例:

以秒为单位输入您要测量的时间(静止): 10

以km / s输入速度: 270000

所以如果你是一艘宇宙飞船并以270000.0公里/秒的速度旅行。并启动一个时间为10.0秒的时钟。 但与此同时,固定的宇宙飞船同时启动一个完全相同的时钟并进行测量 相同的时间。移动的宇宙飞船测得10.0秒后。然后静止 宇宙飞船将测量23.009606245564093秒。因此,移动太空船的时间变得越来越慢。

2 个答案:

答案 0 :(得分:1)

突然出现一些事情。

  1. 使C平方不变(即静态最终),而不是 每次计算它。
  2. 不接受“c”作为执行方程式方法的输入
  3. 制作一个单独的方法,该方法在几秒钟内获取一个长参数,并以最大的适用单位返回一个描述性字符串(例如,1000000 =&gt;“11.57天”)。在此函数中,首先检查年份,因此在后续检查中,您只需要检查参数是否大于数字而不是两个数字之间。
  4. 摆脱main方法中的一系列“if”语句,只有一个语句在上面的#3中调用新函数。

答案 1 :(得分:0)

首先是使用javadoc提高可读性:

    /**
     * Calculates the result of Einsteins time dilation equation. 
     * @param t time in seconds
     * @param v speed in km/s
     * @param c light speed constant
     */
    public void executeEquation(double t, double v, double c) {

    }
  • 在eclipse中使用格式化程序进行正确识别(Ctrl-Shift-F)。

  • 变量v通常具有单位m / s。如果您使用kilomter / s,您可以重命名它:double vKmPerSec。应该对所有不是的单位进行此操作 在SI(国际系统)单位(米,秒,公斤)。

  • &#34;亚斯&#34;对我来说不是一个明显的类名。将其重命名为更具描述性的内容。

  • 类Yass没有评论,您可以将自己添加为作者

  • 结果不应存储在静态变量中。 &#34; dilated_time&#34;

最好使用结果类:

class EinsteinResult {
         // here you store dilated_time, etc.
}

并将结果返回

EinsteinResult executeEquation(double t, double v, double c) {
      //... 
      return EinsteinResult;
}

正如Peter Lawrey评论的那样,将Math.pow(c,2)替换为c*c