import java.util.Scanner;
public class Lab11d
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
double [] anArray; // declares an array of integers
anArray = new double [5];
int min=0;
//Initalizes the array values//
System.out.println ("Enter 5 numbers of your choosing");
double a = in.nextDouble();
for ( int count=0; count < 5; count++)
{
anArray[count] = a;
a = in.nextDouble();
}
//Prints array values//
for (double value : anArray)
System.out.println ("Element at index " + (min++) + ":"+ value + "" );
}
}
它运行,但我只想输入5个数字,不知道我做错了什么。它允许我输入六个限制为5,好奇如何改变它
答案 0 :(得分:1)
因为你的循环之前有一个double
。将其更改为
// double a = in.nextDouble();
for (int count=0; count < 5; count++)
{
double a = in.nextDouble();
anArray[count] = a;
}
或完全消除a
for (int count=0; count < 5; count++)
{
anArray[count] = in.nextDouble();
}
答案 1 :(得分:0)
你接受双倍的第一次,然后迭代一个循环,接受5个双打,即你总共接受6个双打。
您必须将for循环编辑为
for ( int count=0; count < 5; count++)
{
anArray[count] = in.nextDouble();
}