繁体   English   中英

在 Java 中,我需要用户输入五个整数然后显示平均值。 如何在不输入第 6 个条目的情况下获得总和中包含的第 5 个整数?

[英]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?

这是我的代码:

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);
                 
    }

}

这是我当前的输出:

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.

在您的代码中,50 永远不会添加到总和原因中,因为当循环终止时coun5
改为这样做,这将解决您的问题。

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.");
    }

输出:

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.

添加sum+=score; 就在 while 循环之后。

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);
             
}

稍微移动代码以使输入逻辑更清晰。 我们输入 5 个数字,然后计算总和、平均值和计数。

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.");

    }
}

输出:

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.

由于您的问题已经得到解答,但这里要告诉您的是,您从1开始计数,并在计数达到5时结束。

while 循环将在 count 达到5时结束执行,因此在这种情况下,最后一个值不会添加到总和中。

这里有两个修复程序,您可以尝试其中任何一个。

  1. count=1更改为count=0

  2. 在 while 循环sum=+score;之后写这个sum=+score;

请记住使用此解决方案中的任何一种。

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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