Java 连续循环直到满足条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39730789/
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
Continuous Loop until Condition is met
提问by Drewmann
So I have to create a java project in eclipse for my class. The Assignment is to create a program that will allow the user to enter integers into the program until a certain integer is enter (42). After the integer (42) is entered, the program will then 1. average all numbers entered. 2. display the min and max number entered. 3. Total number of numbers entered. And these have to be calculated without counting the (42). This is what i have so far. I can get input from the user and once they enter 42 the program stops and will display the total but with 42 included. And i'm not sure how i could add in a "count" for the input so i can use the count to display total numbers entered as well as dividing count from sum to give me the average. I appreciate any help.
所以我必须在eclipse中为我的班级创建一个java项目。任务是创建一个程序,允许用户在程序中输入整数,直到输入某个整数 (42)。输入整数 (42) 后,程序将变为1。平均所有输入的数字。2. 显示输入的最小和最大数字。3. 输入的数字总数。这些必须在不计算 (42) 的情况下计算。这是我到目前为止。我可以从用户那里获得输入,一旦他们输入 42,程序就会停止并显示总数,但包括 42。而且我不确定如何为输入添加“计数”,以便我可以使用计数来显示输入的总数以及将计数与总和相除以获得平均值。我很感激任何帮助。
package assignment6;
import java.util.Scanner;
public class assignment6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Scanner
Scanner input = new Scanner(System.in);
//Enter integer
System.out.println("Enter a integer ");
int data = input.nextInt();
//Accept integers until 42 is entered
int sum = 42;
while (data != 42) {
sum += data;
System.out.println("Enter another integer ");
data = input.nextInt();
}
//Display sum of numbers entered
System.out.println("The sum of all numbers entered is: " + sum);
//Display Average of numbers entered
System.out.println("Average of all numbers entered is:" + ( sum/ ));
}
}
采纳答案by Aditya Pansare
Check Below Code.
检查下面的代码。
package stackoverflow;
import java.util.Scanner;
public class LoopTest {
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number");
int input = 0;
double sum = 0;
double avg = 0;
int count = 0;
//Infinite Loop
while (true)
{
//Accept Number
input = scanner.nextInt();
//Check Entered Number
if (input != 42)
{
sum = sum + input ;
count = count + 1;
avg = sum/count;
}
else
break;
}
System.out.println("Numbers Entered : " + count);
System.out.println("Sum of Numbers : " + sum);
System.out.println("Average of Numbers : " + avg);
}
}
回答by Matthew
if sum is set to 0, you could count the 42.
如果 sum 设置为 0,则可以计算 42。
Also, use while(true)
to loop forever, and break;
to end the loop. nothing after the break statment is executed, so
此外,使用while(true)
永远循环,并break;
结束循环。执行 break 语句后什么也没有,所以
int count = 1;
while(true) {
sum += data;
System.out.println("Enter another integer ");
data = input.nextInt();
count++;
if(data == 42) {
break;
}
}
回答by KobiF
Well to add a count all you would have to do is make a variable
好吧,添加一个计数,您所要做的就是创建一个变量
int count = 0;
For example and then just add one to it every time the loop repeats
例如,然后每次循环重复时向其添加一个
回答by Iheb Issaoui
import java.util.Scanner;
public class assignment6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Scanner
Scanner input = new Scanner(System.in);
//Enter integer
System.out.println("Enter a integer ");
int data = input.nextInt();
int sum = 0 ;
int count = 0 ;
//Accept integers until 42 is entered
while (data != 42) {
sum += data;
count += 1;
System.out.println("Enter another integer ");
data = input.nextInt();
}
//Display sum of numbers entered
System.out.println("The sum of all numbers entered is: " + sum);
//Display Average of numbers entered
System.out.println("Average of all numbers entered is:" + ( sum/count ));
}
}