传递字符串到数组Java Eclipse

时间:2017-04-17 19:00:08

标签: java

我有一个java代码,它成功运作!

//data 
double[] query = {0.65,0.78,0.21,0.29,0.58};

但现在我希望用户像这样把字符串手册:

println("insert the values like that {xx,xx,xx,xx,xx} ")

我认为必须使用扫描仪,但我不知道如何?

3 个答案:

答案 0 :(得分:0)

如果您想从用户那里获得双号,请使用nextDouble()方法,如下所示:

创建扫描仪对象:

 Scanner scan = new Scanner(System.in);

从用户那里读取输入的数字:

Double insertedDouble = scan.nextDouble();
Double insertedDouble2 = scan.nextDouble();

制作一系列输入的数字:

double[] array = {insertedDouble, insertedDouble2};

答案 1 :(得分:0)

如果要从用户输入中输入,请使用扫描仪:

Scanner scanner = new Scanner(System.in);

确保初始化数组:

private double[] array = new double[10];

提示用户输入:

System.out.println("Please enter double value: ");
array[0] = scanner.nextDouble(); //gets input from user using scanner, store in first index of array

打印:

System.out.println(array[0]);

答案 2 :(得分:0)

直接使用扫描仪试试这个:

import java.util.Scanner;
public class Main {
   public static void main(String[] arguments) {
         // Set the number of values
         int values = 5;

         // Create an empty array for values
         double query[] = new double[values];

         // Create scanner object to use
         Scanner input = new Scanner(System.in);


         // Parse Items for input
         for(int i = 0; i < values; ++i) {
             System.out.println("Please input value num in 0.0 format ");
             query[i] = input.nextDouble();
         }

        // Print the values for success
        for(int i = 0; i < values; ++i)
            System.out.println(query[i]);
    }
}