Java 编写一个程序,不断接收整数,直到给出一个负整数。然后取所有正整数并取它们的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39321325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a program that keeps taking in integers until a negative integer is given. Then take all the positive integers and get the average of them all
提问by Cathal Brady
What i am trying to do is use a while loop to check if int number is positive or negative and depending if such number is positive or negative execute the relevant code. The user is going to keep putting in numbers until they enter a negative number and once they do that the while loop will exit and it will print out the average of all the positive numbers before that negative number was typed in. I was trying to get int number to be reassigned each time the user puts in a number.
我想要做的是使用 while 循环来检查 int 数是正数还是负数,并根据该数是正数还是负数执行相关代码。用户将继续输入数字,直到他们输入一个负数,一旦他们这样做,while 循环将退出,它将在输入负数之前打印出所有正数的平均值。我试图得到每次用户输入数字时要重新分配的 int 数字。
this is my code:
这是我的代码:
import java.util.*;
public class Question7{
public static void main (String [] args){
int number=0;
int average=0;
int counter=0;
int sum=0;
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
while (number > 0) {
counter++;
sum = sum + number;
average= sum/counter;
}
System.out.println("this is the average:" + average);
}
}
采纳答案by Malith
You need to add number = sc.nextInt();
inside the while loop. Otherwise it would be an infinite loop.
您需要number = sc.nextInt();
在 while 循环内添加 。否则就是无限循环。
Another point you have missed is integer division. Most of the above answers have missed it too. Both your counter
variable and sum
variable are integers. Therefore the division will also be an integer division.
您错过的另一点是整数除法。上面的大多数答案也错过了。您的counter
变量和sum
变量都是整数。因此,除法也将是整数除法。
consider
考虑
sum = 17;
counter = 4;
average would be 17/4=4
平均值为 17/4=4
Therefore average should be a double. And you have to cast either sum or counter to double.
因此平均值应该是两倍。并且您必须将 sum 或 counter 转换为双倍。
int average = 0;
average = (double)sum/counter;
回答by Raptor
Your while loop keeps going until counter
reachs Integer
limit. Then your counter will become 0 and an exception will be thrown.
while循环一直走,直到counter
reachsInteger
限制。然后您的计数器将变为 0 并抛出异常。
You need to move number = sc.nextInt();
line into while loop. Thus user input will be required in every iteration.
您需要将number = sc.nextInt();
行移动到 while 循环中。因此,每次迭代都需要用户输入。
回答by Arnav Borborah
I have modified your code, and it should be this:
我已经修改了你的代码,应该是这样的:
int average = 0;
int counter = 0;
int sum = 0;
int number = 0;
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
while (number >= 0)
{
counter++;
sum +=number;
number = sc.nextInt();
}
if (counter == 0)
counter = 1;
average = sum/counter;
System.out.println("this is the average:" + average);
A basic explanation, step by step:
一个基本的解释,一步一步:
- User enters a number.
- If number is less than 0, skip to step 6.
- Increment count.
- Set sum to previous sum + user input.
- Go to step 1.
- Print average, which is
sum/counter
.
- 用户输入一个数字。
- 如果数字小于 0,则跳到步骤 6。
- 递增计数。
- 将 sum 设置为之前的 sum + 用户输入。
- 转到步骤 1。
- 打印平均值,即
sum/counter
.
Live Example现场示例
回答by pamcevoy
The main issue is that you need to read input inside the loop using number = sc.nextInt();
preferably at the end of the loop.
主要问题是您需要number = sc.nextInt();
最好在循环结束时使用读取循环内的输入。
Other than that, what if the user enters a zero? Break the loop or not? Should the average be a float or double instead of an int? You don't need to compute the average until the end. It is good to tell the user what to input and it's good to close the scanner.
除此之外,如果用户输入零怎么办?打破循环与否?平均值应该是浮点数还是双精度数而不是整数?直到最后您才需要计算平均值。告诉用户输入什么好,关闭扫描仪也好。
public static void main (String [] args){
int number = 0;
int counter = 0;
int sum = 0;
// prompt the user
String promptMsg = "Enter an integer (negative to quit): ";
System.out.println(promptMsg);
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
while (number >= 0) {
counter++;
sum += number;
// prompt the user to enter the next input
System.out.println(promptMsg);
number = sc.nextInt();
}
// defer computing average until here
double average = sum / counter;
System.out.println("this is the average:" + average);
// close up when done
sc.close();
}