简体   繁体   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?

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.在您的代码中,50 永远不会添加到总和原因中,因为当循环终止时coun5
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;添加sum+=score; just after the while loop.就在 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);
             
}

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.我们输入 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.");

    }
}

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 .由于您的问题已经得到解答,但这里要告诉您的是,您从1开始计数,并在计数达到5时结束。

while loop will end execution when count reaches 5 so in this condition the last value will not be added to the sum. while 循环将在 count 达到5时结束执行,因此在这种情况下,最后一个值不会添加到总和中。

Here are two fixes, you can try any of these.这里有两个修复程序,您可以尝试其中任何一个。

  1. Change count=1 to count=0count=1更改为count=0

  2. write this after while loop sum=+score;在 while 循环sum=+score;之后写这个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);
}

暂无
暂无

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

相关问题 如何获得字符串中的第5个字? 爪哇 - How do I get the 5th word in a string? Java 如何在Java中获得第5个根? - How can I get the 5th root in Java? 我需要从java中给定的字符串打印第1个字符和第5个字符 - I need to print 1st and every 5th character from a given String in java 如何将一个字符串的第一个字符与 Java 中另一个字符串的第 5 个字符进行比较? 并检查它们是否相同。我收到如下错误 - how to compare first character of one string to 5th char of another string in Java? and check if they are same.I am getting error as below Java - 猜数字游戏。 用户有 6 次尝试。 到目前为止,当输入第 6 次尝试时,游戏就关闭了 - Java - Guess a number game. User has 6 attempts. I've gotten so far as when the 6th attempt is input the game just shuts down 如何使用do-while循环获得五个数字的总和,平均值,最小值和最大值-java - how to get the sum, average, minimum and maximum of five numbers-java using do-while loop 我无法在我的代码中添加第5列 - I am not able to add 5th column in the my code 如何在每第 5 个数字后开始一个新行? - How can I start a new line after every 5th number? 如何使用只有类名的 xpath 选择曾孙(如第 5 个或第 6 个孙子)元素 - How to select grand-grand child (Like 5th OR 6th grand child) element using xpath which has class name only JAVA如何生成1到24之间的整数,但每个整数都需要一对 - JAVA How do I generate integers between 1 and 24 but I need a pair of each integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM