简体   繁体   中英

How would I total two sets of numbers entered by a user?

I am writing a program where I must ask the user how many assignments they have. Then, I must ask them for their score and the maximum points possible for the assignment. I know how to find the sum of the first set of numbers they entered (their scores) but I am stuck on how I would go about totaling the maximum points possible. Here is what I have so far:

      int totalNumber = scan.nextInt();
                double sum = 0.0;
                for (int i = 1; i <= totalNumber; i++) {
                    System.out.print("Assignment " + i + " score and max? ");
                    double score = scan.nextDouble();
                    double maxScore = scan.nextDouble();
                    sum += score;

The output looks something like this: Assignment 1 score and max? 16 17 Assignment 2 score and max? 18 19

I am not sure how I would total the maximum points (17 and 19 in the example) because I must print the total points: (sum of scores)/(sum of maximum points).

Thanks.

the simple answer is to add another variable for summing the maxScore

 int totalNumber = scan.nextInt();
 double sum = 0.0;
 double maxSum = 0.0;
 for (int i = 1; i <= totalNumber; i++) {
     System.out.print("Assignment " + i + " score and max? ");
     double score = scan.nextDouble();
     double maxScore = scan.nextDouble();
     sum += score;
     maxSum += maxScore;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM