Java中用户输入的平均数字
我试图得到用户输入的数字的平均值。每次我输入数字,输出总是- 总数是6 多少个数字:4 平均:2.0 需要while循环,不幸的是不允许for循环。我对Java还很陌生,所以我的格式很松散。我的代码中哪里有问题Java中用户输入的平均数字,java,Java,我试图得到用户输入的数字的平均值。每次我输入数字,输出总是- 总数是6 多少个数字:4 平均:2.0 需要while循环,不幸的是不允许for循环。我对Java还很陌生,所以我的格式很松散。我的代码中哪里有问题 import java.util.Scanner; public class LoopsEndingRemembering { public static void main(String[] args) { // program in this projec
import java.util.Scanner;
public class LoopsEndingRemembering {
public static void main(String[] args) {
// program in this project exercises 36.1-36.5
// actually this is just one program that is split in many parts
Scanner reader = new Scanner(System.in);
int count = 0;
int sum = 0;
int endingVariable = -1;
int input = 0;
double average;
while (input >= endingVariable) {
System.out.println("Type numbers: ");
input = Integer.parseInt(reader.nextLine());
sum += count;
average = (double)sum / count;
count++;
if (input == endingVariable) {
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + count);
System.out.println("Average: " + average);
break;
}
}
}
}
试试这个
import java.util.Scanner;
public class LoopsEndingRemembering {
public static void main(String[] args) {
// program in this project exercises 36.1-36.5
// actually this is just one program that is split in many parts
Scanner reader = new Scanner(System.in);
int count = 0;
int sum = 0;
int endingVariable = -1;
int input = 0;
double average;
while (input >= endingVariable) {
System.out.println("Type numbers: ");
input = Integer.parseInt(reader.nextLine());
sum += input; // you were doing mistake here.
average = (double)sum / count;
count++;
if (input == endingVariable) {
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + count);
System.out.println("Average: " + average);
break;
}
}
}
}
这里有几个错误 导致错误的主要原因是
sum+=count
。您添加的不是用户输入的数字,而是显示有多少个数字的数字。所以它总是加上0+1+2+3等等
另一个问题是,一旦您更改了上述内容,您必须在将输入添加到总和之前检查输入==endingVariable
。因此,您必须先编写if
,然后计算其中的平均值。否则,它会将您的-1
视为要计算的数字序列的一部分
如果发现输入与endingVariable
不同,只需将输入添加到总和并增加计数
因此:
- 将输入(而不是计数)添加到总和
- 只有达到最后一项时,才计算平均值
- 仅当输入不是
时,才将输入添加到总和并递增计数器endingVariable
- 您的代码有两个大问题。首先,这一行:
sum += count;
这可能只是一个打字错误,但显然应该是:
sum += input;
第二,一旦你解决了这个问题,你会注意到你的总数中包含了-1。你可能想要这样的东西:
System.out.println("Type numbers: ");
input = Integer.parseInt(reader.nextLine());
if (input == endingVariable) {
average = (double) sum / count;
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + count);
System.out.println("Average: " + average);
break;
} else {
sum += input;
count++;
}
谢谢你的详细解释。它现在运行良好,我所要做的就是你说的更改输入,并将sum+=输入和计数移到else中最后一项之后。我一直在努力弄清楚为什么会包括my-1您是否使用了调试器并逐步完成了该程序?为什么要将
count
添加到sum
而不是input
?您从未使用输入做过任何事情
!另外,在递增count
之前计算平均值,因此,例如,当您输入第二个数字时,计算平均值时,count
仍然是1
,而不是2
。的可能重复项也应该使用双倍。@tnw不一定。只要您只是添加,使用int
或long
就可以了。当你到达平均部分时,你只需要做一次double
。或者,如果您的金额有可能如此之高,它将溢出。感谢您的详细解释。它现在运行良好,我所要做的就是你说的更改输入,并将sum+=输入和计数移到else中最后一项之后。我正试图找出为什么我的1是被包括在内。最困难的是,如果OP想输入一个非整数??NoahKettler。如果我的答案对你有帮助,请考虑接受它。