How to put a Scanner input into an array… for example a couple of numbers

后端 未结 11 1696
青春惊慌失措
青春惊慌失措 2020-11-30 05:20
Scanner scan = new Scanner(System.in);
double numbers = scan.nextDouble();
double[] avg =..????
相关标签:
11条回答
  • 2020-11-30 05:24
    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:24
    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();
    
    0 讨论(0)
  • 2020-11-30 05:38

    This is a program to show how to give input from system and also calculate sum at each level and average.

    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);
      }
     }
    
    0 讨论(0)
  • 2020-11-30 05:39
    double [] avg = new double[5];
    for(int i=0; i<5; i++)
       avg[i] = scan.nextDouble();
    
    0 讨论(0)
  • 2020-11-30 05:40
    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();
    
    }
    
    0 讨论(0)
  • 2020-11-30 05:41

    You can get all the doubles with this code:

    List<Double> numbers = new ArrayList<Double>();
    while (scan.hasNextDouble()) {
        numbers.add(scan.nextDouble());
    }
    
    0 讨论(0)
提交回复
热议问题