Java 将双精度存储在数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31467064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:09:18  来源:igfitidea点击:

Storing doubles in an array

javaarrays

提问by Code123

Alright, so I want to store values in an array. Here is my code :

好的,所以我想将值存储在数组中。这是我的代码:

import java.util.Scanner;
public class test {
    public static void main(String args[]){
        Scanner bob = new Scanner(System.in);

        double[] mylist;

        System.out.println("What is your first value?");
        mylist = bob.nextDouble();

        System.out.println("What is your second value?");
        mylist = bob.nextDouble();
    }
}

Issue #1: I'm being told mylist = bob.nextDouble()should be nextLine(). Why is that if it is clearly a double?

问题 #1:我被告知mylist = bob.nextDouble()应该是nextLine(). 如果它显然是双重的,为什么会这样?

Goal: I want to somehow store those values given into an array so that I could later pull them out. Can anyone help?

目标:我想以某种方式将这些给定的值存储到一个数组中,以便我以后可以将它们取出。任何人都可以帮忙吗?

UPDATE

更新

For Issue #1, it tells me that it cannot convert from Double()to Double[]. What does this mean?

对于问题 #1,它告诉我它不能从 转换Double()Double[]. 这是什么意思?

采纳答案by Tot Zam

mylistis an arrayof doubles, not a single double.

mylistarray双打,不是单打double

When you do mylist = bob.nextDouble();you are basically trying to make an entire array equal a single number.

当您这样做时,mylist = bob.nextDouble();您基本上是在尝试使整个数组等于一个数字。

You instead need to assign the number to a specific place in the array.

您需要将数字分配到数组中的特定位置。

  1. Initialize the array with the amount of elements you are going to store in the array:

    double[] mylist = new double[2];
    
  2. Assign the double to a specific place in the array:

    mylist[0] = bob.nextDouble();
    mylist[1] = bob.nextDouble();
    
  1. 使用要存储在数组中的元素数量初始化数组:

    double[] mylist = new double[2];
    
  2. 将 double 分配给数组中的特定位置:

    mylist[0] = bob.nextDouble();
    mylist[1] = bob.nextDouble();
    

回答by Elliott Frisch

It's an array (which is a type of object). First, create an array large enough to store your double(s) (not int(s)). And store the values at appropriate indices. Something like,

它是一个数组(它是一种对象)。首先,创建一个足够大的数组来存储您的double(s)(不是int(s))。并将值存储在适当的索引处。就像是,

Scanner bob = new Scanner(System.in);
double[] mylist = new double[2];
System.out.println("What is your first value?");
mylist[0] = bob.nextDouble();
System.out.println("What is your second value?");
mylist[1] = bob.nextDouble();
System.out.println(Arrays.toString(mylist));

回答by Dici

An array is not a double, you need to select an index :

数组不是双精度数,您需要选择一个索引:

double[] arr = new double[2];
System.out.println("What is your first value?");
arr[0] = bob.nextDouble();

System.out.println("What is your second value?");
arr[1] = bob.nextDouble();

Also, don't call an array a list, use CamelCase for naming your variables/methods/classes, and initialize the array. I recommend you reading a Java tutorial for working on the basics of the language.

另外,不要将数组称为列表,使用 CamelCase 命名变量/方法/类,并初始化数组。我建议您阅读 Java 教程以了解该语言的基础知识。

回答by Juned Ahsan

The error is because you are trying to assign a double value to array in the following statement:

错误是因为您试图在以下语句中为数组分配双精度值:

    mylist = bob.nextDouble();

as mylist is defined as an array here:

因为 mylist 在这里被定义为一个数组:

    double[] mylist;

First you need to intialize your array:

首先你需要初始化你的数组:

    double[] mylist = new double[2]; // initialise array to have two elements

and then try to assign the array elements with the user input like this:

然后尝试使用用户输入分配数组元素,如下所示:

    mylist[0] = bob.nextDouble();
    mylist[1] = bob.nextDouble();

回答by Samrat Dutta

Better still, use a list with generics.

更好的是,使用带有泛型的列表。

List<Double> myList;

myList.add(bob.nextDouble());

That way, you can add as many doubles you want to the list. So that you dont have to worry about the indexing.

这样,您可以在列表中添加任意数量的双打。这样您就不必担心索引。