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?

huangapple go评论69阅读模式
英文:

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("请输入一个测试分数:");
        score = scoreInput.nextInt();
        
        while (count < 5) {
            sum += score;
            count++;
            System.out.print("请输入一个测试分数:");
            score = scoreInput.nextInt();
        }
        
        System.out.println("你的测试分数平均值:" + sum/count);
        System.out.println("你输入了 " + count + " 个正整数。");
        System.out.println("总和为 " + sum);
    }
}

这是我的当前输出:

请输入一个测试分数:10
请输入一个测试分数:20
请输入一个测试分数:30
请输入一个测试分数:40
请输入一个测试分数:50
你的测试分数平均值:30.0
你输入了 5 个正整数。
总和为 150.0
// 这应该总计为 150,平均值为 30。
英文:

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(&quot;Please enter a test score &quot;);
		score = scoreInput.nextInt();
		
		 while (count &lt; 5) {
	         sum += score;
	         count++;
	         System.out.print(&quot;Please enter a test score &quot;);
	         score = scoreInput.nextInt();
		 }
		deafult:
			System.out.println(&quot;Your test score average: &quot; + sum/count);
		 	System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
		 	System.out.println(&quot;The sum is &quot; + 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.

答案1

得分: 1

在你的代码中,50 从未被添加到总和中,因为一旦 count 达到 5,while 循环就会终止。改成以下方式即可修复问题。

public static void main(String args[]) {
    int score;
    double sum = 0;
    int count = 0; // 改变初始值为0
    
    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(); // 获取下一个分数
    }
    
    System.out.println("Your test score average: " + sum/count);
    System.out.println("You entered " + (count + 1) + " positive integers."); // 将 count + 1 以修复打印错误
    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 6 positive integers.
The sum is 150.0 positive integers.
英文:

In your code 50 is never added to the sum cause soon as coun is 5 while loop is terminated.<br>
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(&quot;Please enter a test score &quot;);
	         score = scoreInput.nextInt();
		     
                 // changed
                 while (count &lt; 5) {
		         
		         System.out.print(&quot;Please enter a test score &quot;);
		         score = scoreInput.nextInt();
		         sum += score;
		         count++;
		     }
		         System.out.println(&quot;Your test score average: &quot; + sum/count);
		         System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
		         System.out.println(&quot;The sum is &quot; + sum + &quot; positive integers.&quot;);
	}

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.

答案2

得分: 1

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

public static void main(String[] args) {

    int score;
    double sum = 0;
    int count = 1;

    Scanner scoreInput = new Scanner(System.in);

    System.out.print("请输入一个测试分数 ");
    score = scoreInput.nextInt();

    while (count < 5) {
        sum += score;
        count++;
        System.out.print("请输入一个测试分数 ");
        score = scoreInput.nextInt();
    }
    sum+=score;
    deafult:
        System.out.println("您的测试分数平均值: " + sum/count);
        System.out.println("您输入了 " + count + " 个正整数。");
        System.out.println("总和为 " + sum);

}
英文:

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(&quot;Please enter a test score &quot;);
    score = scoreInput.nextInt();
    
     while (count &lt; 5) {
         sum += score;
         count++;
         System.out.print(&quot;Please enter a test score &quot;);
         score = scoreInput.nextInt();
     }
     sum+=score;
    deafult:
        System.out.println(&quot;Your test score average: &quot; + sum/count);
        System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
        System.out.println(&quot;The sum is &quot; + sum);
             
}

答案3

得分: 0

将代码稍微调整以使输入逻辑更清晰。我们输入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("请输入一个测试分数:");
            score = scoreInput.nextInt();
            sum += score;
            count++;
        }
        // count增加了1,多了一个数的计数。因此,减少1。
        count--;
        System.out.println("您的测试分数平均值:" + sum / count);
        System.out.println("您输入了 " + count + " 个正整数。");
        System.out.println("总和为 " + sum + " 个正整数。");
    }
}

输出:

请输入一个测试分数:10
请输入一个测试分数:20
请输入一个测试分数:30
请输入一个测试分数:40
请输入一个测试分数:50
您的测试分数平均值:30.0
您输入了 5 个正整数。
总和为 150.0 个正整数。
英文:

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 &lt;= 5) {
            System.out.print(&quot;Please enter a test score &quot;);
            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(&quot;Your test score average: &quot; + sum/count);
        System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
        System.out.println(&quot;The sum is &quot; + sum + &quot; positive integers.&quot;);

    }
}

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.

答案4

得分: 0

因为你的问题已经得到了回答,但是我想告诉你的是,你从 1 开始计数,直到计数达到 5

while loop 会在计数达到 5 时结束执行,因此在这个条件下,最后一个值不会被加到总和中。

以下是两个修复方法,你可以尝试任选一个:

  1. count=1 改为 count=0

  2. 在 while 循环后面加上这行代码 sum=+score;

请记住使用这两个解决方案中的任何一个。

英文:

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.

答案5

得分: 0

问题出在您的while循环中。我们可以在while循环中看到,它接收了分数值,但只将其添加到总和中,用于下一个计数值。

例如,让我们看看以下示例代码:

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

在这个例子中,count < 2 成立,所以开始时总和sum为0。然后执行以下操作:

  1. sum += val1(无论您输入什么值)
  2. count++count 变成2)
  3. score 被赋值为输入的 val2(无论您输入什么值)

问题在于,您将值放入了score,但只在下一轮将此值添加到总和sum中。在这个示例中,val2 将不会被添加到总和sum中。

请看下面这个修正后的示例代码:

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(); // 不需要写 sum+=score,因为它是 score 的第一个值
        
         while (count < 5) {
             System.out.print("Please enter a test score ");
             score = scoreInput.nextInt();
             sum += score;
             count++;
         }
         
         // 注意:下面的部分应该是 switch 而不是 deafult
         switch (count) {
            case 1: // 如果输入的只有一个分数
                System.out.println("You entered only one test score.");
                System.out.println("Your test score is " + sum);
                break;
            default:
                System.out.println("Your test score average: " + sum/count);
                System.out.println("You entered " + count + " positive integers.");
                System.out.println("The sum is " + sum);
                break;
        }
                 
    }

}

请注意,我修正了代码中的一些错误,使其可以正确地计算分数的总和和平均值。

英文:
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&#39;s take    

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

while count &lt; 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(&quot;Please enter a test score &quot;);
        
        sum = scoreInput.nextInt();//no need to write sum+=score cause it&#39;s the first value of score 
        
         while (count &lt; 5) {
             System.out.print(&quot;Please enter a test score &quot;);
             score = scoreInput.nextInt();
             sum += score;
             count++;
         }
        deafult:
            System.out.println(&quot;Your test score average: &quot; + sum/count);
            System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
            System.out.println(&quot;The sum is &quot; + sum);
                 
    }

}

答案6

得分: 0

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 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 &gt; 0) {
			System.out.print(&quot;Please enter a test score &quot;);
            score = scoreInput.nextInt();
            sum += score;
            totalCount --;
            count++;                    
        }
        System.out.println(&quot;Your test score average: &quot; + sum/count);
        System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
		System.out.println(&quot;The sum is &quot; + sum);                         
    }        
}

答案7

得分: -2

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("请输入一个测试分数:");
        score = scoreInput.nextInt();
        sum += score;
        count++;
    }
    default:
        System.out.println("您的测试分数平均值:" + sum/count);
        System.out.println("您输入了" + count + "个正整数。");
        System.out.println("总和为" + sum);
}
英文:
public static void main(String[] args) {
    int score;
    double sum = 0;
    int count = 0;
            
    Scanner scoreInput = new Scanner(System.in);
   
     while (count &lt; 5) {
         System.out.print(&quot;Please enter a test score &quot;);
         score = scoreInput.nextInt();
         sum += score;
         count++;
     }
    deafult:
        System.out.println(&quot;Your test score average: &quot; + sum/count);
        System.out.println(&quot;You entered &quot; + count + &quot; positive integers.&quot;);
        System.out.println(&quot;The sum is &quot; + sum);
}

huangapple
  • 本文由 发表于 2020年9月28日 11:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64095734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定