[英]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 永远不会添加到总和原因中,因为当循环终止时
coun
为5
。
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.这里有两个修复程序,您可以尝试其中任何一个。
Change count=1
to count=0
将
count=1
更改为count=0
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.