0

I'm trying to make an average calculator for children and depending on how many children there are I want to calculate it. So you can see my code the max amount of children can be 10 and there are 10 cases, but it won't let me use the same variables from the other cases.

Any tips on how to resolve this issue?

package day1.examples;

import java.util.Scanner;


public class Practice2 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("This program will calculate the average age of children. First we need to know how many children there are before we calculate the average. ");
        System.out.println("Enter the number of children");
        int NumberOfChildren;
        NumberOfChildren = keyboard.nextInt();
        switch (NumberOfChildren) {
        case 1:
            System.out.println("What is the age of Child 1");
            double one = keyboard.nextDouble();
            System.out.println("The average age of child 1 is " + one);
            break;
        case 2:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            double average = (one + two)/2;
            System.out.println("The average age of the children is " + average);
            break;
        case 3:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            double average = (one + two + three)/3;
            System.out.println("The average age of the children is " + average);
            break;
        case 4:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            double average = (one + two + three + four)/4;
            System.out.println("The average age of the children is " + average);
            break;
        case 5:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            System.out.println("What is the age of child 5");
            double five = keyboard.nextDouble();
            double average = (one + two + three + four + five)/5;
            System.out.println("The average age of the children is " + average);
            break;
        case 6:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            System.out.println("What is the age of child 5");
            double five = keyboard.nextDouble();
            System.out.println("What is the age of child 6");
            double six = keyboard.nextDouble();
            double average = (one + two + three + four + five + six)/6;
            System.out.println("The average age of the children is " + average);
            break;
        case 7:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            System.out.println("What is the age of child 5");
            double five = keyboard.nextDouble();
            System.out.println("What is the age of child 6");
            double six = keyboard.nextDouble();
            System.out.println("What is the age of child 7");
            double seven = keyboard.nextDouble();
            double average = (one + two + three + four + five + six + seven)/7;
            System.out.println("The average age of the children is " + average);
            break;
        case 8:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            System.out.println("What is the age of child 5");
            double five = keyboard.nextDouble();
            System.out.println("What is the age of child 6");
            double six = keyboard.nextDouble();
            System.out.println("What is the age of child 7");
            double seven = keyboard.nextDouble();
            System.out.println("What is the age of child 8");
            double eight = keyboard.nextDouble();
            double average = (one + two + three + four + five + six + seven + eight)/8;
            System.out.println("The average age of the children is " + average);
            break;
        case 9:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            System.out.println("What is the age of child 5");
            double five = keyboard.nextDouble();
            System.out.println("What is the age of child 6");
            double six = keyboard.nextDouble();
            System.out.println("What is the age of child 7");
            double seven = keyboard.nextDouble();
            System.out.println("What is the age of child 8");
            double eight = keyboard.nextDouble();
            System.out.println("What is the age of child 9");
            double nine = keyboard.nextDouble();
            double average = (one + two + three + four + five + six + seven + eight + nine)/9;
            System.out.println("The average age of the children is " + average);
            break;
        case 10:
            System.out.println("What is the age of child 1?");
            double one = keyboard.nextDouble();
            System.out.println("What is the age of child 2?");
            double two = keyboard.nextDouble();
            System.out.println("What is the age of child 3");
            double three = keyboard.nextDouble();
            System.out.println("What is the age of child 4");
            double four = keyboard.nextDouble();
            System.out.println("What is the age of child 5");
            double five = keyboard.nextDouble();
            System.out.println("What is the age of child 6");
            double six = keyboard.nextDouble();
            System.out.println("What is the age of child 7");
            double seven = keyboard.nextDouble();
            System.out.println("What is the age of child 8");
            double eight = keyboard.nextDouble();
            System.out.println("What is the age of child 9");
            double nine = keyboard.nextDouble();
            System.out.println("What is the age of child 10");
            double ten = keyboard.nextDouble();
            double average = (one + two + three + four + five + six + seven + eight + nine + ten)/10;
            System.out.println("The average age of the children is " + average);        
            break;
        default: 
            System.out.println("Invalid amount of children");
            break;  
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
haidder
  • 1
  • 1
  • 1
    Using switch statements to do this is a really brute force approach. Have you learned for loops and arrays yet? – paisanco Mar 13 '16 at 23:51
  • This can be solved simply with a loop and *without* any arrays - [averages can be computed on-the-fly](http://stackoverflow.com/questions/12636613/how-to-calculate-moving-average-without-keeping-the-count-and-data-total). Each time through the loop ask for the age of the nth child and update the (current) average. (Any time that something needs to be done 1..N times usually indicates a loop would be useful.) – user2864740 Mar 13 '16 at 23:53
  • Now, if "wanting" to keep the code the same - which I would *not* recommend, as it is overly repetitive/brittle - the variables can be scoped with [a block](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html), as so: `case n: { double one = .. /* other block-scoped variables here */ }` – user2864740 Mar 13 '16 at 23:58

2 Answers2

0

Have you tried a for with an array? Something like this:

int amount = ...
double[] values = new double[amount];
for (int i = 0; i < amount; i++) {
    System.out.println("What is the age of child 1?");
    values[i] = keyboard.nextDouble();
}

Then you can iterate again for average and etc.

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
0

Using an array is a good idea. But to fix your actual error, put braces around each case block.

Buddy
  • 10,874
  • 5
  • 41
  • 58