Reputation: 11
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 + "" );
}
}
It runs, but I only want to input 5 numbers, not sure what I am doing wrong. It allows me to enter six with like a limit of 5, curious how to change it please
Upvotes: 0
Views: 31
Reputation: 327
You are accepting a double first time and then iterating through a loop which accepts 5 doubles i.e. in total you are accepting 6 doubles.
You have to edit your for loop as,
for ( int count=0; count < 5; count++)
{
anArray[count] = in.nextDouble();
}
Upvotes: 0
Reputation: 201447
Because you get one double
before your loop. Change it to something like
// double a = in.nextDouble();
for (int count=0; count < 5; count++)
{
double a = in.nextDouble();
anArray[count] = a;
}
or eliminate a
altogether like
for (int count=0; count < 5; count++)
{
anArray[count] = in.nextDouble();
}
Upvotes: 1