簡體   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