JAVA: if 语句在 for 循环内并退出 for 循环
JAVA: if statement inside a for loop and exiting from a for loop
大家好,我是新手。我正在为我的 prog 单元做作业,所以请耐心等待。
所以我要做的是编写一个代码,可以输入人的年龄,从 1 到 120 之间的整数(含 1 到 120)。然后用户必须计算平均年龄,并且应该计算为实数。但是用户必须输入年龄值,直到用户输入 0,即停止程序然后输出平均值。如果用户输入的年龄无效,则程序应继续重新提示用户,直到他们输入有效年龄。
所以我尽了自己的一份力量。我创建了一个代码并想出了这个:
public static void main(String[] args)
{
int ageValue = 0;
double getAge;
getAge = inputAge();
System.out.println("Average age is: " + getAge);
}
public static double inputAge()
{
int ageValue = 0;
double avgAge = 0;
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 120; i++)
{
System.out.println("Enter age");
ageValue += sc.nextInt();
avgAge = ageValue / (double) i;
if (ageValue == 0)
{
System.out.println("Average age is: " + avgAge);
System.exit(0);
}
while (ageValue < 0 || ageValue > 120)
{
System.out.println("Invalid input. Try again!");
ageValue = sc.nextInt();
}
}
return avgAge;
}
现在我放下了我的代码,我的平均公式以某种方式起作用了。现在,问题是当我按 0 时,它不会提示 "if" 语句。但是,当出现第一个 "Enter your age" 提示并且我按下 0 时,"if" 语句起作用了。但是对于每次迭代,程序都不会让我执行语句。
另一方面,我也在努力弄清楚如何在不使用 break 或 System.exit() 的情况下退出循环,因为那样会给我零分。我想要的是当我按下 0 时,它应该退出循环并输出平均值,就像任务所说的那样。
不知道大家能不能看懂。。代码对吗?我在正确的轨道上吗?我错过了什么吗???
干杯
On the other hand, I am also struggling to figure out how to exit a loop without using break or System.exit() because that will give me zero marks.
你可以像这样在你的 for 循环中有另一个条件
boolean finished = false;
for (int i = 1; i <= 120 && finished == false; i++)
并替换
System.exit(0)
和
finished = true;
但是,我会质疑为什么使用 "break" 会给你打零分。这正是我们想要的那种场景中断。
你可以试试这个方法。
我已经更正了一些退出条件和完成平均的方式。
您在代码中显示的 "for" 循环将样本数限制为 120,但问题没有这么说,所以我冒昧地将您的问题概括为任意数量的样本平均。
首先你应该查看 "if-else" 条件结构,因为这是你的代码中缺少的要点。
https://en.wikipedia.org/wiki/Conditional_(computer_programming)
你可以把问题的表达方式想成:
- 计算意甲的平均值
- 系列为键盘输入
- 输入零时,退出循环,return当前平均值
- 当输入任何超出范围[0,120]的值时,给出消息并继续循环而不改变任何值
当输入 [1,119] 范围内的任何值时,将该值添加到系列并重新计算平均值
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
System.out.println("Final Average age is: "+inputAge());
}
private static double inputAge()
{
int ageValue=0;
double avgAge=0;
boolean shouldExit=false;
Scanner sc=new Scanner(System.in);
List<Integer> samples=new ArrayList<Integer>();
// loop until flag is true
while(!shouldExit)
{
System.out.println("Enter age");
ageValue=sc.nextInt();
if(ageValue==0)
{
shouldExit=true;
}
else if(ageValue<0||ageValue>120)
{
System.out.println("Invalid input. Try again!");
}
else
{
// add current input in the samples and calculate average over all the samples
samples.add(ageValue);
avgAge=getAvg(samples);
System.out.println("Current Average age is: "+avgAge);
}
}
sc.close();
return avgAge;
}
private static double getAvg(List<Integer> samples)
{
double avgAge=0;
for(Integer tmp:samples)
{
avgAge+=tmp;
}
return avgAge/(double) samples.size();
}
}
您可以考虑 do while
循环方法。这将使您的代码自然 运行 一次,并在用户输入 0
:
后退出
int ageValue = 0, numOfAges = 0, sumOfAges = 0;
do {
System.out.println("Enter age");
ageValue = sc.nextInt();
if (ageValue < 0 || ageValue > 120)
System.out.println("Bad value... try again");
else if (ageValue != 0) {
sumOfAges += ageValue;
numOfAges++;
}
} while (ageValue != 0);
return ((double)sumOfAges / numOfAges);
大家好,我是新手。我正在为我的 prog 单元做作业,所以请耐心等待。
所以我要做的是编写一个代码,可以输入人的年龄,从 1 到 120 之间的整数(含 1 到 120)。然后用户必须计算平均年龄,并且应该计算为实数。但是用户必须输入年龄值,直到用户输入 0,即停止程序然后输出平均值。如果用户输入的年龄无效,则程序应继续重新提示用户,直到他们输入有效年龄。
所以我尽了自己的一份力量。我创建了一个代码并想出了这个:
public static void main(String[] args)
{
int ageValue = 0;
double getAge;
getAge = inputAge();
System.out.println("Average age is: " + getAge);
}
public static double inputAge()
{
int ageValue = 0;
double avgAge = 0;
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 120; i++)
{
System.out.println("Enter age");
ageValue += sc.nextInt();
avgAge = ageValue / (double) i;
if (ageValue == 0)
{
System.out.println("Average age is: " + avgAge);
System.exit(0);
}
while (ageValue < 0 || ageValue > 120)
{
System.out.println("Invalid input. Try again!");
ageValue = sc.nextInt();
}
}
return avgAge;
}
现在我放下了我的代码,我的平均公式以某种方式起作用了。现在,问题是当我按 0 时,它不会提示 "if" 语句。但是,当出现第一个 "Enter your age" 提示并且我按下 0 时,"if" 语句起作用了。但是对于每次迭代,程序都不会让我执行语句。
另一方面,我也在努力弄清楚如何在不使用 break 或 System.exit() 的情况下退出循环,因为那样会给我零分。我想要的是当我按下 0 时,它应该退出循环并输出平均值,就像任务所说的那样。
不知道大家能不能看懂。。代码对吗?我在正确的轨道上吗?我错过了什么吗???
干杯
On the other hand, I am also struggling to figure out how to exit a loop without using break or System.exit() because that will give me zero marks.
你可以像这样在你的 for 循环中有另一个条件
boolean finished = false;
for (int i = 1; i <= 120 && finished == false; i++)
并替换
System.exit(0)
和
finished = true;
但是,我会质疑为什么使用 "break" 会给你打零分。这正是我们想要的那种场景中断。
你可以试试这个方法。
我已经更正了一些退出条件和完成平均的方式。
您在代码中显示的 "for" 循环将样本数限制为 120,但问题没有这么说,所以我冒昧地将您的问题概括为任意数量的样本平均。
首先你应该查看 "if-else" 条件结构,因为这是你的代码中缺少的要点。
https://en.wikipedia.org/wiki/Conditional_(computer_programming)
你可以把问题的表达方式想成:
- 计算意甲的平均值
- 系列为键盘输入
- 输入零时,退出循环,return当前平均值
- 当输入任何超出范围[0,120]的值时,给出消息并继续循环而不改变任何值
当输入 [1,119] 范围内的任何值时,将该值添加到系列并重新计算平均值
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("Final Average age is: "+inputAge()); } private static double inputAge() { int ageValue=0; double avgAge=0; boolean shouldExit=false; Scanner sc=new Scanner(System.in); List<Integer> samples=new ArrayList<Integer>(); // loop until flag is true while(!shouldExit) { System.out.println("Enter age"); ageValue=sc.nextInt(); if(ageValue==0) { shouldExit=true; } else if(ageValue<0||ageValue>120) { System.out.println("Invalid input. Try again!"); } else { // add current input in the samples and calculate average over all the samples samples.add(ageValue); avgAge=getAvg(samples); System.out.println("Current Average age is: "+avgAge); } } sc.close(); return avgAge; } private static double getAvg(List<Integer> samples) { double avgAge=0; for(Integer tmp:samples) { avgAge+=tmp; } return avgAge/(double) samples.size(); } }
您可以考虑 do while
循环方法。这将使您的代码自然 运行 一次,并在用户输入 0
:
int ageValue = 0, numOfAges = 0, sumOfAges = 0;
do {
System.out.println("Enter age");
ageValue = sc.nextInt();
if (ageValue < 0 || ageValue > 120)
System.out.println("Bad value... try again");
else if (ageValue != 0) {
sumOfAges += ageValue;
numOfAges++;
}
} while (ageValue != 0);
return ((double)sumOfAges / numOfAges);