Подтвердить что ты не робот

Как поместить вход сканера в массив... например, пару цифр

Scanner scan = new Scanner(System.in);
double numbers = scan.nextDouble();
double[] avg =..????
4b9b3361

Ответ 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();
    }
}

Кажется, это довольно простой материал, если я не понимаю вас.

Ответ 2

Вы можете получить все удваивания с помощью этого кода:

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

Ответ 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

Здесь код:

    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");
            }

        }

    }

Подумайте, как это сделать

Ответ 5

**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+" ,");
    }
}

Ответ 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

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();

Ответ 8

double [] avg = new double[5];
for(int i=0; i<5; i++)
   avg[i] = scan.nextDouble();

Ответ 9

Это программа, показывающая, как вводить данные из системы, а также рассчитывать сумму на каждом уровне и в среднем.

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);
  }
 }

Ответ 10

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();

}

Ответ 11

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);
    }
}

Ответ 12

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();
    }
}