- #1
- 265
- 0
Homework Statement
I need to create a loop that asks the user to input double numbers into an array. The number of elements in the array is not predefined; the user will continue to enter numbers as long as they wish until a NEGATIVE number is entered, at which point the control will exit the loop used for inputting.
Homework Equations
None
The Attempt at a Solution
My problem is this: my understanding is that an array can't do anything, ie can't be used, until the control knows how much memory to allocate for that array.
import java.util.Scanner;
public class ClassifyScores2
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.println("This program classifies scores into different ranges.");
System.out.println("Enter your scores now, enter a negative number when you wish to stop:");
for (ind = 0; new score[ind] >= 0 ; ind++)
{
score[ind] = input.nextInt();
//if(score[ind] > 100)
//System.out.println("This score is too high. Enter a score less than or equal to 100");
//else if(score[ind] < 0)
//System.exit(1);
//else
//score2[ind] = score[ind]
the commented if statements I can do myself, I'm not concerned with that. I will close the loop later on. My program runs and compiles if I define the array score somewhere earlier --> there are no syntax errors in my source code except what is in bold.
I am able to ask the user "How many numbers will you be inputting?" and then store that number, and then use that in the loop. That way, the control knows how many numbers will be entered. But the assignment strictly says "The user of your program can enter as many scores as (s)he wishes. The user identifies the end by entering a negative number for score; your program then should display the tabulated scores and exit."
This must be followed. The only way I can see this is by declaring my "score" array INSIDE my FOR loop. I have no clue how to do this! Is this possible? Is there any other way of doing this?
Thank you so much in advance!