简体   繁体   中英

In Java, I need the user to enter five integers then show the average. How do I get the 5th integer included in the sum without typing a 6th entry?

Here is my code:

import java.util.Scanner;

public class Assignment8TommyDuke {

    public static void main(String[] args) {
        
        int score;
        double sum = 0;
        int count = 1;
                
        Scanner scoreInput = new Scanner(System.in);
            
        System.out.print("Please enter a test score ");
        score = scoreInput.nextInt();
        
         while (count < 5) {
             sum += score;
             count++;
             System.out.print("Please enter a test score ");
             score = scoreInput.nextInt();
         }
        deafult:
            System.out.println("Your test score average: " + sum/count);
            System.out.println("You entered " + count + " positive integers.");
            System.out.println("The sum is " + sum);
                 
    }

}

and here is my current output:

Please enter a test score 10

Please enter a test score 20

Please enter a test score 30

Please enter a test score 40

Please enter a test score 50

Your test score average: 20.0

You entered 5 positive integers.

The sum is 100.0

//This should total 150 with an average of 30.

In your code 50 is never added to the sum cause soon as coun is 5 while loop is terminated.
Do this instead and this will fix your problem.

public static void main(String args[]) {
           int score;
            double sum = 0;
            int count = 1;
                    
            Scanner scoreInput = new Scanner(System.in);
               
            System.out.print("Please enter a test score ");
             score = scoreInput.nextInt();
             
                 // changed
                 while (count < 5) {
                 
                 System.out.print("Please enter a test score ");
                 score = scoreInput.nextInt();
                 sum += score;
                 count++;
             }
                 System.out.println("Your test score average: " + sum/count);
                 System.out.println("You entered " + count + " positive integers.");
                 System.out.println("The sum is " + sum + " positive integers.");
    }

Output:

Please enter a test score 10
Please enter a test score 20
Please enter a test score 30
Please enter a test score 40
Please enter a test score 50
Your test score average: 25.0
You entered 6 positive integers.
The sum is 150.0 positive integers.

add sum+=score; just after the while loop.

public static void main(String[] args) {
    
    int score;
    double sum = 0;
    int count = 1;
            
    Scanner scoreInput = new Scanner(System.in);
        
    System.out.print("Please enter a test score ");
    score = scoreInput.nextInt();
    
     while (count < 5) {
         sum += score;
         count++;
         System.out.print("Please enter a test score ");
         score = scoreInput.nextInt();
     }
     sum+=score;
    deafult:
        System.out.println("Your test score average: " + sum/count);
        System.out.println("You entered " + count + " positive integers.");
        System.out.println("The sum is " + sum);
             
}

Moved the code around around a little to make the input logic cleaner. We input 5 numbers and then calculate the sum, average and count.

public class Test {
    public static void main(String[] args) {
        int score;
        double sum = 0;
        int count = 1;
        Scanner scoreInput = new Scanner(System.in);
        while (count <= 5) {
            System.out.print("Please enter a test score ");
            score = scoreInput.nextInt();
            sum += score;
            count++;
        }
        //count has increased by 1 more than the count of numbers. Hence, decrease by 1.
        count--;
        System.out.println("Your test score average: " + sum/count);
        System.out.println("You entered " + count + " positive integers.");
        System.out.println("The sum is " + sum + " positive integers.");

    }
}

Output:

Please enter a test score 10
Please enter a test score 20
Please enter a test score 30
Please enter a test score 40
Please enter a test score 50
Your test score average: 30.0
You entered 5 positive integers.
The sum is 150.0 positive integers.

Since your question is already answered, but here is what is want to tell you is this, you are starting count from 1 and ending when count reaches 5 .

while loop will end execution when count reaches 5 so in this condition the last value will not be added to the sum.

Here are two fixes, you can try any of these.

  1. Change count=1 to count=0

  2. write this after while loop sum=+score;

remember use any one of this solution.

the problem exist in ur while loop we could see that in the while loop it receive the score value but it only add it to sum for the next value of count
for example let's take    

    while(count<2){
    sum += score;
    count++;
    System.out.print("Please enter a test score ");
    score = scoreInput.nextInt();                       
    }

while count < 2 
so starting with sum containing 0 value sum=0
sum+=val1 (what ever you enter)
count ++ (count =2)
score = input of val2 (what ever you enter)

the problem here that you are putting values inside score but only adding this value to sum in the next round  in this example the val2 will be not assigned to the sum  

have a look at this one 
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        int score;
        double sum = 0;
        int count = 1;
                
        Scanner scoreInput = new Scanner(System.in);
        System.out.print("Please enter a test score ");
        
        sum = scoreInput.nextInt();//no need to write sum+=score cause it's the first value of score 
        
         while (count < 5) {
             System.out.print("Please enter a test score ");
             score = scoreInput.nextInt();
             sum += score;
             count++;
         }
        deafult:
            System.out.println("Your test score average: " + sum/count);
            System.out.println("You entered " + count + " positive integers.");
            System.out.println("The sum is " + sum);
                 
    }

}
public class Assignment8TommyDuke {        
    public static void main(String[] args) {                
        int score;
        double sum = 0;
        int totalCount = 5,count = 0;                        
        Scanner scoreInput = new Scanner(System.in);                            
        while (totalCount > 0) {
            System.out.print("Please enter a test score ");
            score = scoreInput.nextInt();
            sum += score;
            totalCount --;
            count++;                    
        }
        System.out.println("Your test score average: " + sum/count);
        System.out.println("You entered " + count + " positive integers.");
        System.out.println("The sum is " + sum);                         
    }        
}
public static void main(String[] args) {
    int score;
    double sum = 0;
    int count = 0;
            
    Scanner scoreInput = new Scanner(System.in);
   
     while (count < 5) {
         System.out.print("Please enter a test score ");
         score = scoreInput.nextInt();
         sum += score;
         count++;
     }
    deafult:
        System.out.println("Your test score average: " + sum/count);
        System.out.println("You entered " + count + " positive integers.");
        System.out.println("The sum is " + sum);
}

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