所以我已经编程了一段时间,而且我对程序的重要性感到困惑。格式良好"我写了这个简单的公式程序。怎么可能"更好"写的?在程序中,您可以为爱因斯坦时间膨胀方程输入一些数字,并输出在一定时间后扩张了多少时间。
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秒。因此,移动太空船的时间变得越来越慢。
答案 0 :(得分:1)
突然出现一些事情。
答案 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