我需要制作一个程序,该程序在运行程序运行时,自Unix Epoch以来,涉及光线已经运行的距离(在真空中)的距离(在真空中)。
- 字符串:"由于Unix时期,光线已经旅行了……"。
- 公里数的数量,假设光的速度正好为299792458 m/s:x.xxx km。
- 假设149597870700.0 m/au:x.xxx au。
- 光年数,假设9460730472580800.0 m/ly:x.xxx ly。
- parsecs的数量,假设3.085677581E16 m/pc(请注意,在正常科学符号中,3.085677581e16是Java表示3.085677581·10^16的方式):X.xxx PC。
在我的代码中,我会在整数过高,并且使用打印方法遇到麻烦。谁能帮忙?
我遇到的错误是:
Assignment02.java:16: error: integer number too large: 9460730472580
long ly = km / 9460730472580;
^
Assignment02.java:20: error: ')' expected
"Since the Unix epoch, light has traveled..."
^
Assignment02.java:25: error: not a statement
(km), au, ly, pc);
^
Assignment02.java:25: error: ';' expected
(km), au, ly, pc);
^
Assignment02.java:25: error: not a statement
(km), au, ly, pc);
^
Assignment02.java:25: error: ';' expected
(km), au, ly, pc);
^
Assignment02.java:25: error: not a statement
(km), au, ly, pc);
^
Assignment02.java:25: error: ';' expected
(km), au, ly, pc);
^
Assignment02.java:25: error: not a statement
(km), au, ly, pc);
^
Assignment02.java:25: error: ';' expected
(km), au, ly, pc);
这是我的代码:
public class Assignment02 {
public static void main (String[] args) {
// Stores the return value of System.currentTimeMillis() in variab$
long now = System.currentTimeMillis();
// Calculation of how far light has travelled until "now" in kilometer$
long km = (299792 * now)/1000 ;
// Calculatuion of how far light has travelled until "now" in astronomical units$
long au = km / 149597870;
// Calculation of how far light has travelled until "now" in light years$
long ly = km / 9460730472580;
// Calculation of how far light has traveled until "now" in parsecs
long pc = km / 3.085677581e16;
System.out.printf(
"Since the Unix epoch, light has traveled..."+
"%d km"+
"%d au"+
"%d ly"+
"%d pc",
(km), au, ly, pc);
}
}
您可以修复编译错误和这样的打印语句:
- 将
L
添加到大整数值的结束中,以使其成为long
字面。 - 将铸件添加到
long
中以转换double
值。 - 添加了
%n
以打印格式字符串以在输出中添加新线。
long now = System.currentTimeMillis();
long km = (299792 * now)/1000 ;
long au = km / 149597870;
long ly = km / 9460730472580L;
long pc = (long) (km / 3.085677581e16);
System.out.printf("Since the Unix epoch, light has traveled...%n"+
"%d km%n"+
"%d au%n"+
"%d ly%n"+
"%d pc",
(km), au, ly, pc);
当然,您实际上可能需要输出double
值,而不是long
值:
long now = System.currentTimeMillis();
long km = (299792 * now)/1000 ;
long au = km / 149597870;
double ly = km / 9460730472580d;
double pc = km / 3.085677581e16;
System.out.printf("Since the Unix epoch, light has traveled...%n"+
"%d km%n"+
"%d au%n"+
"%.1f ly%n"+
"%f pc",
(km), au, ly, pc);
输出
Since the Unix epoch, light has traveled...
451349163977648 km
3017082 au
47.7 ly
0.014627 pc
整数太大
编译代码时,您会收到此错误:
:12: error: integer number too large: 9460730472580
long ly = km / 9460730472580;
^
1 error
编译器告诉您,您要划分的数字太大而无法成为整数,因此您需要使用较大的数据类型。
在这种情况下,您可以告诉编译器通过附加l后缀,明确地说它是很长的:
long ly = km / 9460730472580L;
打印方法
您的打印方法不会仅仅因为您将字符串拆分为代码中的字符串。您需要使用诸如"%n"
之类的行分隔符或使用System.out.println
之类的东西将输出放在单独的行上。
可能的精度丧失
但是,即使您修复了"整数数量太大"错误,由于此错误,您仍然不会编译:
:14: error: possible loss of precision
long pc = km / 3.085677581e16;
^
required: long
found: double
这是因为您的除法操作会导致双重操作,并且您正在尝试将其塞入长时间。您的选项要么是将pc
的数据类型更改为double
(并更新您的打印语句),要么将计算施加到long
:
// either change the data type:
double pc = km / 3.085677581e16;
// or cast the result:
long pc = (long)(km / 3.085677581e16);
精确度损失很重要的原因是,parsec的数量实际上小于1。由于您将该值存储为长时间,因此最终将以0为0。
这就是为什么您可能要考虑将此字段的数据类型切换到双重,以便您可以更准确地显示计算结果。
结果
您的输出看起来像这样:
Since the Unix epoch, light has traveled...
451349237824811 km
3017083 au
47 ly
0 pc
使用BigInteger
(和/或BigDecimal
)而不是long
,类似的东西:
public class Assignment02
{
public static void main( String[] args )
{
// Stores the return value of System.currentTimeMillis() in variab$
final BigInteger now = BigInteger.valueOf( System.currentTimeMillis() );
// Calculation of how far light has travelled until "now" in kilometer$
final BigInteger km = BigInteger.valueOf( 299792 ).multiply( now )
.divide( BigInteger.valueOf( 1000 ) );
// Calculatuion of how far light has travelled until "now" in astronomical units$
final BigInteger au = km.divide( BigInteger.valueOf( 149597870 ) );
// Calculation of how far light has travelled until "now" in light years$
final BigInteger ly = km.divide( BigInteger.valueOf( 9460730472580L ) );
// Calculation of how far light has traveled until "now" in parsecs
final BigDecimal pc = new BigDecimal( km )
.divide( BigDecimal.valueOf( 3085677581L, -7 ),
new MathContext( 5 ) );
System.out.printf( "Since the Unix epoch, light has traveled...%n" +
"%d km%n%d au%n%d ly%n%f pc%n",
km, au, ly, pc );
}
输出:
Since the Unix epoch, light has traveled...
451349466051964 km
3017084 au
47 ly
0,014627 pc