【问题标题】:How to put a Scanner input into an array... for example a couple of numbers如何将扫描仪输入放入数组中......例如几个数字
【发布时间】:2010-05-08 19:34:07
【问题描述】:
Scanner scan = new Scanner(System.in);
double numbers = scan.nextDouble();
double[] avg =..????

【问题讨论】:

  • 条目数是已知还是未知?

标签: java arrays java.util.scanner


【解决方案1】:

你可以试试这样的:

public static void main (String[] args)
{
    Scanner input = new Scanner(System.in);
    double[] numbers = new double[5];

    for (int i = 0; i < numbers.length; i++)
    {
        System.out.println("Please enter number");
        numbers[i] = input.nextDouble();
    }
}

除非我误解了你,否则这似乎很基本的东西

【讨论】:

  • 当您超过阵列容量 (5) 时,这将不起作用。更好的选择是使用列表。
  • @MarkHughes:确实,但是 OP 要求提供一个数组。
  • 这就是为什么我把它作为评论而不是答案。这些问题通常出现在搜索结果中:)
【解决方案2】:

您可以使用以下代码获得所有双打:

List<Double> numbers = new ArrayList<Double>();
while (scan.hasNextDouble()) {
    numbers.add(scan.nextDouble());
}

【讨论】:

  • 我们如何将字符串添加到List,我使用了这个代码while(scanner.hasNext()){ list.add(scanner.next().toString());},但它给了我NullPointerException
【解决方案3】:
import java.util.Scanner;

public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner in=new Scanner (System.in);
        int num[]=new int[10];
        int average=0;
        int i=0;
        int sum=0;

        for (i=0;i<num.length;i++) {
            System.out.println("enter a number");
            num[i]=in.nextInt();
            sum=sum+num[i];
        }
        average=sum/10;
        System.out.println("Average="+average);
    }
}

【讨论】:

  • 这可用于获取输入的平均值
【解决方案4】:
**Simple solution**
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int size;
    System.out.println("Enter the number of size of array");
    size = sc.nextInt();
    int[] a = new int[size];
    System.out.println("Enter the array element");
    //For reading the element
    for(int i=0;i<size;i++) {
        a[i] = sc.nextInt();
    }
    //For print the array element
    for(int i : a) {
        System.out.print(i+" ,");
    }
}

【讨论】:

  • 代码总是好的,但它也有助于提供一些关于您的解决方案解决原始问题的 cmets。这是进一步改进这个和未来答案的建议。
  • 谢谢哥们,所以现在写它更好或一些其他格式问题再次非常感谢。
【解决方案5】:

对于由空格分隔" " 给出的数组值,您可以尝试这个很酷的基于 Java 8 及更高版本支持流的解决方案:

Scanner scan = new Scanner(System.in);
double[] arr = Arrays.stream(scan.nextLine()
                                  .trim()
                                  .split(" "))
                                  .filter(x -> !x.equals(""))
                                  .mapToDouble(Double::parseDouble)
                                  .toArray();

对于int数组你可以试试:

 Scanner scan = new Scanner(System.in);
 int[] arr = Arrays.stream(scan.nextLine()
                                  .trim()
                                  .split(" "))
                                  .filter(x -> !x.equals(""))
                                  .mapToInt(Integer::parseInt)
                                  .toArray();

使用filter() 方法,您还可以避免输入之间出现多个空格。

完整的代码参考:

import java.util.Scanner;
import java.util.Arrays;

public class Main{  
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double[] arr = Arrays.stream(scan.nextLine()
                                    .trim()
                                    .split(" "))
                                    .filter(x -> !x.equals(""))
                                    .mapToDouble(Double::parseDouble)
                                    .toArray();
                              
        for(double each: arr){
            System.out.print(each + "  ");
        }
    }
 }

输入:1 2 3 4 5 6 7 8 9

输出:1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

如果你仔细观察,输入之间随机有多余的空格,这也是用.filter(x -&gt; !x.equals(""))行处理的,以避免空白输入(""

【讨论】:

  • 您能否发布整个代码 sn-p 以及 Scanner 类的实例化、正确的导入和结果的呈现?
  • @JacobTheKnitter ,我添加了代码解释以便更好地理解。
【解决方案6】:
import  java.util.Scanner;

class Array {
public static void main(String a[]){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter the size of an Array");

    int num = input.nextInt();

    System.out.println("Enter the Element "+num+" of an Array");

    double[] numbers = new double[num];

    for (int i = 0; i < numbers.length; i++)
    {

        System.out.println("Please enter number");

        numbers[i] = input.nextDouble();

    }

    for (int i = 0; i < numbers.length; i++)
    {

        if ( (i%3) !=0){

            System.out.print("");

            System.out.print(numbers[i]+"\t");

        } else {
            System.out.println("");

            System.out.print(numbers[i]+"\t");
        }

    }

}

【讨论】:

    【解决方案7】:
    double [] avg = new double[5];
    for(int i=0; i<5; i++)
       avg[i] = scan.nextDouble();
    

    【讨论】:

      【解决方案8】:

      这是一个程序,用于展示如何从系统提供输入并计算每个级别的总和和平均值。

      package NumericTest;
      
      import java.util.Scanner;
      
      public class SumAvg {
      
      
       public static void main(String[] args) {
      
       int i,n;
       System.out.println("Enter the number of inputs");
       Scanner sc = new Scanner(System.in);
       n=sc.nextInt();
       int a[] = new int [n];
      
          System.out.println("Enter the inputs");
         for(i=0;i<n;i++){
         a[i] = sc.nextInt();
        System.out.println("Inputs are " +a[i]);
       }
      
        int sum = 0;
        for(i=0;i<n;i++){
       sum = sum +a[i];
        System.out.println("Sums : " +sum);
       }
        int avg ;
        avg = sum/n;
        System.out.println("avg : " +avg);
        }
       }
      

      【讨论】:

        【解决方案9】:
        List<Double> numbers = new ArrayList<Double>();
        double sum = 0;
        
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            double value = scan.nextDouble();
            numbers.add(value);
            sum += value;
        }
        
        double average = sum / numbers.size();
        

        【讨论】:

          【解决方案10】:
          public static void main (String[] args)
          {
              Scanner s = new Scanner(System.in);
              System.out.println("Please enter size of an array");
              int n=s.nextInt();
              double arr[] = new double[n];
              System.out.println("Please enter elements of array:");
              for (int i=0; i<n; i++)
              {
                  arr[i] = s.nextDouble();
              }
          }
          

          【讨论】:

          • 你的答案和选择的答案有什么区别?
          【解决方案11】:

          如果您不知道数组的大小,则需要定义何时停止读取序列(在下面的示例中,程序在用户引入 0 时停止,显然,不应考虑最后一个零帐户)。

          import java.util.Scanner;
          import java.util.ArrayList;
          
          public class Main
          {
          
            public static void main (String[]args)
            {
                  
                  System.out.println("Introduce the sequence of numbers to store in array. Each of the introduced number should be separated by ENTER key. Once you're done, type in 0.");
                  Scanner scanner = new Scanner(System.in);
                  ArrayList<Integer> numbers = new ArrayList<Integer>();
                  boolean go = true; 
                  while (go) {
                      int value = scanner.nextInt();
                      numbers.add(value);
                      if (value == 0) {
                          go = false;
                          numbers.remove(numbers.size() - 1);
                      }
                  }
                  System.out.println(numbers);
            }
          }
          

          【讨论】:

            【解决方案12】:
            Scanner scan = new Scanner (System.in);
            
            for (int i=0; i<=4, i++){
            
                System.out.printf("Enter value at index"+i+" :");
            
                anArray[i]=scan.nextInt();
            
            }
            

            【讨论】:

              【解决方案13】:
              import java.util.Scanner;
              public class sort {
              
                public static void main(String args[])
                  {
                      int i,n,t;          
              
                      Scanner sc=new Scanner(System.in);
              
                      System.out.print("Enter the size of array");
              
                      n=sc.nextInt();
              
                      int a[] = new int[n];
              
                      System.out.println("Enter elements in array");
              
                      for(i=0;i<n;i++)
                      {
                          a[i]=sc.nextInt();
                      }
                      t=a[1];
              
                      for(i=0;i<n;i++)
                      {
                          if(a[i]>t)
              
                              t=a[i];
                      }
                      System.out.println("Greates integer is" +t);
                  }
              }
              

              【讨论】:

              • 虽然您的回答可能会解决问题,但最好能说明问题所在以及您的回答如何解决问题。这是进一步改进这个和未来答案的建议。
              猜你喜欢
              • 1970-01-01
              • 2016-06-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多