【问题标题】:How do I get this array of double to print without error?如何让这个 double 数组打印而不会出错?
【发布时间】:2016-11-11 20:04:45
【问题描述】:

我得到了一个复制和粘贴的代码,它是下面的代码,除了将 double 替换为 int。我应该把它变成双倍,所以我更换了所有东西,但仍然收到可能的有损转换错误。各位大佬知道怎么回事吗?

public class InitializingNumericgArray
{
    public static void main(String [] args)
    {
      double [] doubleValues;
      doubleValues = new double[10];

      for(double n = 0; n <= 9; n+= 1)
        {
           System.out.println("index position " + n + " = "
           + doubleValues[n]);
        }
    }
}

【问题讨论】:

  • 数组的索引是整数。不要为此使用双精度数。
  • 数组的索引是整数...相应地改变你的 for 循环
  • 建议添加语言标签。
  • 我改变了for循环,谢谢

标签: java arrays compiler-errors double


【解决方案1】:

错误是:

  1. 数组的声明语法不正确

  2. 循环变量 n 必须是整数,因为 n 的所有值都转换为 double ex。 0 变为 0.0,1 变为 1.0,依此类推。因此下一行代码

    doubleValues[n] //被解释为doubleValues[1.0]这是错误的

这是带有更正的代码:

public class InitializingNumericgArray
{
     public static void main(String []args)
     {
          double []doubleValues;
          doubleValues = new double[10];

          for(int n = 0; n <= 9; n+= 1)
          {
               System.out.println("index position " + n + " = "
                + doubleValues[n]);
          }
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2020-10-08
    • 2021-03-01
    • 1970-01-01
    • 2021-04-03
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多