用于计算光线行进距离的代码出错

时间:2017-09-16 05:31:53

标签: java

我需要制作一个程序,该程序生成5行输出,说明自Unix时代以来,在程序运行时光的行进距离(在真空中)。

  1. 字符串:“自从Unix时代以来,光已经走过......”。
  2. 公里数,假设光速恰好为299792458 m / s:x.xxx km。
  3. 天文单位数,假设为149597870700.0 m / au:x.xxx au。
  4. 光年数,假设为9460730472580800.0 m / ly:x.xxx ly。
  5. 秒数的数量,假设为3.085677581e16 m / pc(注意3.085677581e16是Java在正常科学计数法中表示3.085677581⋅10^ 16的方式): x.xxx pc。
  6. 在我的代码中,我得到的错误是整数太高而且使用print方法时遇到问题。有人可以帮忙吗?

    我得到的错误是:

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

3 个答案:

答案 0 :(得分:3)

您可以修复编译错误和打印语句,如下所示:

  • 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

答案 1 :(得分:1)

整数过大

编译代码时,会出现此错误:

:12: error: integer number too large: 9460730472580
    long ly = km / 9460730472580;
                   ^
1 error

编译器告诉您,除以的数字太大而不是整数,因此您需要使用更大的数据类型。

在这种情况下,您可以通过附加L后缀告诉编译器显式为long:

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

精度损失很重要的原因是因为秒差的数量实际上小于1.因为你将该值存储为long,所以最终得到的值为0.

这就是为什么您可能要考虑将此字段的数据类型切换为double,以便更准确地显示计算结果。

结果

您的输出将如下所示:

Since the Unix epoch, light has traveled...
451349237824811 km
3017083 au
47 ly
0 pc

答案 2 :(得分:0)

使用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