dimanche 8 octobre 2017

What might be a simpler way to code this?

I'm writing a program that allows students to calculate the average number of students in each of their classes. At the moment, my code looks like this:

(double classes is the number of classes the student is taking, and the number of students in each class is assigned to class1, class2, etc.)

if (classes == 1) {
    System.out.printf("How many students are there in your 1 st class?%n");
    class1 = TextIO.getlnDouble();
    classes = classes + 1;
    System.out.printf("Do you have another class?");
    boolean response = TextIO.getlnBoolean();
  } else if (classes == 2);

et cetera, for all 9 possible classes. (My school only allows the credit equivalent of 6.5 classes to be taken in a single semester, but some of that may be made up of two-credit or non-credit classes.)

I could just make a loop out of this, but how would I assign the given number to the correct double? i.e. how could I swap out class1 for class2, class3, etc.?

Aucun commentaire:

Enregistrer un commentaire