我正在尝试为孩子们制作一个平均计算器,并且根据我想要计算它的孩子数量。所以你可以看到我的代码最多可以有10个孩子,有10个案例,但它不会让我使用其他案例中的相同变量。
有关如何解决此问题的任何提示?
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;
}
}
}
答案 0 :(得分:0)
您是否尝试过使用数组?像这样:
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();
}
然后你可以再次迭代平均等等。
答案 1 :(得分:0)
使用数组是一个好主意。但要修复实际错误,请在每个class SoftwareHelp(Help):
about_software = models.ForeignKey('Software')
def save(self, *args, **kwargs):
self.about_software = 'Help for %s' % self.title
return super(SoftwareHelp, self).save(*args, **kwargs)
块周围添加大括号。