如何将Scanner输入放入数组中...例如一些数字

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

这是已知数量还是未知数量的条目? - Bozho
13个回答

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

1
你的答案和被选中的答案有什么区别? - Amit Yadav

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

}

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

1
虽然您的回答可能解决了问题,但如果您能提供问题的描述以及您的回答是如何解决它的,那就更好了。这是进一步改善当前和未来回答的建议。 - Luís Cruz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接