如何在用户输入数据后重新运行java代码

嘿,我有一个基本的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 } 

它将从主方法调用

 this.ageCheck(); 

你可以做一段时间(真正的)循环来永远运行

你可以递归调用main()

在此之后in.close(); 声明就像这样调用你的main()

  in.close(); main(null); 

但是你必须在什么时候停止执行你的程序。