# Unknown variables/input?

• 11-09-2013, 08:35 PM
!RobG
Unknown variables/input?
Hello all,

Below is a little program that adds three numbers, finds the average of said numbers and then and assigns a letter grade.

Problem: How do I write the program if the amount of numbers that are to be added and averaged are unknown? i.e - the first time run the program it adds and averages 3 numbers; the next time, it adds and averages 5 numbers.

import java.util.Scanner;

public class Application {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int numbera;
int numberb;
int numberc;

System.out.println("Enter first score");

numbera = input.nextInt();

System.out.println("Enter second score");

numberb = input.nextInt();

System.out.println("Enter third score");

numberc = input.nextInt();

int number = (numbera + numberb + numberc)/3;

System.out.println("Your average score is " + number);

//number = input.nextInt();

if ((number >= 90) && (number <=100))
System.out.println("You got an A");

else if((number >=80) && (number <=89))

System.out.println("You got a B");

else if((number >=70) && (number <=79))
System.out.println("You got a C");

else if((number >=60) && (number <=69))
System.out.println("You got a D");

else if (number <=59)
System.out.println("You got a F");

}
}
• 11-09-2013, 08:43 PM
Norm
Re: Unknown variables/input?
Quote:

if the amount of numbers that are to be added and averaged are unknown
The normal solution is to use a collections class like ArrayList which can hold lots of data. If you must use an array, choose a large size that you are sure won't be exceeded.