嘿,我有一个基本的java“应用程序”,显示人们是成年人还是青少年等。我从java开始,在用户输入年龄和字符串后我找不到如何制作它它们被归类为“我希望它重新运行整个过程,以便其他人可以尝试”的节目。我一直在考虑做一个循环,但这对我来说没有用。这是代码:
public static void main(String[] args)
{
//Declare local variables
int age;
Scanner in = new Scanner(System.in);
//Prompt user for Age
System.out.print("Enter Age: ");
age = in.nextInt();
if(age > 17)
{
System.out.println("This persons age indicates that they an adult");
}
else if (age > 12)
{
System.out.println("This persons age indicates that they are a teenager");
}
else if (age >= 0)
{
System.out.println("This persons age indicates that they are a child");
}
else
{
System.out.println("That age is invalid");
}
//Close input
in.close();
}
任何帮助将不胜感激。我确信这非常简单,但我错过了一些东西。
您可以将其放入循环中,例如
public static void main(String[] args)
{
//Declare local variables
int age;
Scanner in = new Scanner(System.in);
while(true){
//Prompt user for Age
System.out.print("Enter Age: ");
age = in.nextInt();
if(age > 17)
{
System.out.println("This persons age indicates that they an adult");
}
else if (age > 12)
{
System.out.println("This persons age indicates that they are a teenager");
}
else if (age >= 0)
{
System.out.println("This persons age indicates that they are a child");
}
else
{
System.out.println("That age is invalid");
}
}
//Close input
in.close(); //Note that this happens outside the loop
}
或者将所需的代码放入方法中
public static void ageCheck()
{
//code that is currently in main method
}
它将由 main 方法调用
this.ageCheck();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)