在Java中,我需要用户输入五个整数,然后显示平均值。如何在不键入第6个条目的情况下获得总和中包含的第5个整数?
这是我的密码:在Java中,我需要用户输入五个整数,然后显示平均值。如何在不键入第6个条目的情况下获得总和中包含的第5个整数?,java,Java,这是我的密码: 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(
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);
}
}
publicstaticvoidmain(字符串[]args){
智力得分;
双和=0;
整数计数=0;
Scanner scoreInput=新扫描仪(System.in);
同时(计数<5){
系统输出打印(“请输入考试分数”);
score=scoreInput.nextInt();
总和+=分数;
计数++;
}
迪福尔特:
System.out.println(“您的考试分数平均值:+总和/计数”);
System.out.println(“您输入了”+count+“正整数”);
System.out.println(“总和为”+总和);
}
公共静态void main(字符串[]args){
智力得分;
双和=0;
整数计数=0;
Scanner scoreInput=新扫描仪(System.in);
同时(计数<5){
系统输出打印(“请输入考试分数”);
score=scoreInput.nextInt();
总和+=分数;
计数++;
}
迪福尔特:
System.out.println(“您的考试分数平均值:+总和/计数”);
System.out.println(“您输入了”+count+“正整数”);
System.out.println(“总和为”+总和);
}
将代码移动了一点,以使输入逻辑更清晰。我们输入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.");
}
}
稍微移动代码,使输入逻辑更清晰。我们输入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.");
}
}
在代码中,循环终止时,
con
为5
时,决不会将50添加到总和原因中。改为这样做,这将解决您的问题
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.");
}
在代码中,循环终止时,
con
为5
时,决不会将50添加到总和原因中。改为这样做,这将解决您的问题
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.");
}
添加
sum+=分数代码>就在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);
}
publicstaticvoidmain(字符串[]args){
智力得分;
双和=0;
整数计数=1;
Scanner scoreInput=新扫描仪(System.in);
系统输出打印(“请输入考试分数”);
score=scoreInput.nextInt();
同时(计数<5){
总和+=分数;
计数++;
系统输出打印(“请输入考试分数”);
score=scoreInput.nextInt();
}
总和+=分数;
迪福尔特:
System.out.println(“您的考试分数平均值:+总和/计数”);
System.out.println(“您输入了”+count+“正整数”);
System.out.println(“总和为”+总和);
}
添加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);
}
publicstaticvoidmain(字符串[]args){
智力得分;
双和=0;
整数计数=1;
Scanner scoreInput=新扫描仪(System.in);
系统输出打印(“请输入考试分数”);
score=scoreInput.nextInt();
同时(计数<5){
总和+=分数;
计数++;
系统输出打印(“请输入考试分数”);
score=scoreInput.nextInt();
}
总和+=分数;
迪福尔特:
System.out.println(“您的考试分数平均值:+总和/计数”);
System.out.println(“您输入了”+count+“正整数”);
System.out.println(“总和为”+总和);
}
既然你的问题已经得到了回答,但我想告诉你的是,你从1
开始计数,当计数达到5
时结束
当计数达到5
时,循环将结束执行,因此在这种情况下,最后一个值不会添加到总和中
这里有两个修复程序,您可以尝试其中任何一个
将count=1
更改为count=0
在while循环sum=+score之后写这个代码>
请记住使用此解决方案中的任何一种。因为您的问题已经得到了回答,但下面是我们想告诉您的是,您从1
开始计数,当计数达到5
时结束
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);
}
}
当计数达到5
时,循环将结束执行,因此在这种情况下,最后一个值不会添加到总和中
这里有两个修复程序,您可以尝试其中任何一个
将count=1
更改为count=0
在while循环sum=+score之后写这个代码>
请记住使用此解决方案中的任何一个。问题存在于我们的while循环中。我们可以看到,在while循环中,它接收分数值,但它只将其添加到count的下一个值的总和中
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);
}
}
举个例子吧
while(count在ur while循环中存在问题,我们可以看到while循环中它接收到分数值,但它只将其添加到count的下一个值的总和中
举个例子吧
while(count)去掉计数--;
并在0处开始计数
,然后将测试更改回计数<5
。去掉计数--;
并在0处开始计数
,然后将测试更改回计数<5
。
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);
}
}