Results 1 to 2 of 2
  1. #1
    !RobG is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default 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);

    //System.out.println("Enter a grade score: ");

    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");


    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Unknown variables/input?

    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.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2012, 11:04 PM
  2. Creating an amount of variables based on user input
    By HDInfinity in forum New To Java
    Replies: 4
    Last Post: 04-11-2012, 08:23 AM
  3. Replies: 5
    Last Post: 03-17-2011, 04:19 PM
  4. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  5. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •