Java 从循环中提取值?
这是一个程序,用来找出一个人的年龄必须是多少才能成为一年的平方根。如何从循环中提取年龄和关联年份,以便打印Java 从循环中提取值?,java,loops,math,Java,Loops,Math,这是一个程序,用来找出一个人的年龄必须是多少才能成为一年的平方根。如何从循环中提取年龄和关联年份,以便打印 package squareAges; import javax.swing.JOptionPane; import java.util.Scanner; import java.lang.Math; public class SquareAges { public static final int MIN_YEAR = 1893; public static fin
package squareAges;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.Math;
public class SquareAges {
public static final int MIN_YEAR = 1893;
public static final int MAX_YEAR = 2139;
public static void main(String[] args) {
for (int age = 0; age <= 123; age++) {
System.out.println(age + "= " + age * age + " ");
if (MIN_YEAR <= (age * age) && (age * age) <= MAX_YEAR) {
JOptionPane.showMessageDialog(null, "It is possible that someone alive today "
+ "has, is, or will be alive in a year that is the square of their age.");
}
}
}
}
包装面积;
导入javax.swing.JOptionPane;
导入java.util.Scanner;
导入java.lang.Math;
公共类广场{
公共静态最终整年=1893年;
公共静态最终整数最大年份=2139;
公共静态void main(字符串[]args){
for(int age=0;age
在循环外引入局部变量
在if
语句中分配它
在if
语句中执行break
在循环后使用局部变量
如果只想打印满足上述条件的特定值,可以在循环内部使用If,代码如下:
int age, year;
for (year = 1893; year <= 2139; year++) {
for (age = 0; age <= 123; age++) {
System.out.println(age + "= " + age * age + " ");
if (MIN_YEAR <= (age * age) && (age * age) <= MAX_YEAR) {
JOptionPane.showMessageDialog(null, "It is possible that someone alive today "
+ "has, is, or will be alive in a year that is the square of their age.");
if (age * age == year) {
System.out.println("Age is:" + age + "Year is:" + year);
}
}
}
}
int年龄,年份;
for(year=1893;year在for循环外部声明int sqrtAge=0,然后分配sqrtAge=age*age;在它内部,我希望从循环中取出一个特定值,(例如)age 45=year 2025我希望从循环中取出一个特定值,(例如)45岁=2025年根据你的评论编辑了我的答案。希望这有帮助。我希望从循环中提取一个特定的值,(例如)45岁=2025年