import java.util.Scanner;
public class App {
public static void main(String[] args) {
System.out.println("Please enter your official name.");
Scanner typing = new Scanner(System.in);
String name = typing.nextLine();
while (!name.matches("[a-zA-Z]")) {
System.out.println("Please enter your official name.");
name = typing.nextLine();
}
if (name.length() >= 4)
System.out.println("Your name is: " + name);
else
System.out.println("So your name is less than four characters.");
}
}
如何在此处重新启动程序! PS:是Java的初学者
答案 0 :(得分:3)
一旦main方法退出,应用程序就会退出。将您的代码放入另一个方法中,并在while循环中调用它。
public static void main(String[] args) {
while(true){
process();
}
}
static void process(){
System.out.println("Please enter your official name.");
Scanner typing = new Scanner(System.in);
String name = typing.nextLine();
while (!name.matches("[a-zA-Z]")) {
System.out.println("Please enter your official name.");
name = typing.nextLine();
}
if (name.length() >= 4)
System.out.println("Your name is: " + name);
else
System.out.println("So your name is less than four characters.");
}
这将无限次地运行程序。如果要退出,请按cmd中的 Ctrl + C 或可以使用
System.exit(0);
代码中的某个地方。
答案 1 :(得分:1)
要完全重新启动该程序并不容易,而且也不是必需的。您可以用另一种方式做同样的事情。
public static void main(String[] args) {
foo();
}
public static void foo(){
System.out.println("Please enter your official name.");
Scanner typing = new Scanner(System.in);
String name = typing.nextLine();
while (!name.matches("[a-zA-Z]")) {
System.out.println("Please enter your official name.");
name = typing.nextLine();
}
if (name.length() >= 4)
System.out.println("Your name is: " + name);
else
System.out.println("So your name is less than four characters.");
if(true){// put your condition when you what to restart here
foo();// rerun this method
}
}
更新。长时间运行后,以前的解决方案可能会导致错误。 这样更好。
public static void main(String[] args) {
boolean willContinue = false;
do{
System.out.println("Please enter your official name.");
Scanner typing = new Scanner(System.in);
String name = typing.nextLine();
while (!name.matches("[a-zA-Z]")) {
System.out.println("Please enter your official name.");
name = typing.nextLine();
}
if (name.length() >= 4)
System.out.println("Your name is: " + name);
else
System.out.println("So your name is less than four characters.");
willContinue = true;// put your condition when you what to restart here
}while(willContinue);
}
答案 2 :(得分:0)
非常感谢大家,从两个答案中汲取了教训并写下了这段话:
导入java.util.Scanner;
公共类应用{
public static void main(String[] args) {
restart();
}
public static void restart() {
System.out.println("Please enter your official name!");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
String regex = "^[a-zA-Z]+$";
do {
System.out.println("Please enter your official name.");
name = input.nextLine();
} while (name.length() < 11);
if (name.matches(regex)) {
System.out.println("Your name is: " + name);
System.exit(0);
}
else {
System.out.println("That's not a name!");
restart();
}
}
}
答案 3 :(得分:0)
我进一步改进了它:
导入java.util.Scanner;
公共类应用{
public static void main(String[] args) {
restart();
}
public static void restart() {
System.out.println("Please enter your official name!");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
String regex = "^[a-zA-Z]+$";
while (name.length() < 11) {
System.out.println("Please enter your official name.");
name = input.nextLine();
continue;
}
if (name.matches(regex)) {
System.out.println("Your name is: " + name);
System.exit(0);
}
else {
System.out.println("That's not a name!");
restart();
}
}
}