The title says it all, is there a way to stop a method from continuing and making it start over again from its start?
标题说明了一切,有没有办法阻止一个方法继续并让它从一开始就重新开始?
public class Numbers {
static Scanner numberScanner = new Scanner(System.in);
public static int answer;
public static int insertNumbers(int num1, int num2) {
System.out.println("Insert two numbers");
num1 = numberScanner.nextInt();
if (numberScanner.hasNext()) {
num2 = numberScanner.nextInt();
} else {
System.out.println("Equation format incorrect");
//stop method and start over again from "Insert two numbers".
}
answer = num1 + num2;
return answer;
}
}
4 个解决方案
#1
1
This is what i propose:
这就是我的建议:
in the else case, you just use this construction
在else的情况下,你只需使用这种结构
else{
System.out.println("Equation format incorrect");
return insertNumbers();
}
Please be aware the code will keep on asking input until the input is two numbers. No need for the arguments in the method declaration, since you don't use them
请注意,代码将继续询问输入,直到输入为两个数字。不需要方法声明中的参数,因为您不使用它们
#2
1
You can return immediately from that point, by returning an int grom the method, eg -1 if not correct and you know you need to retry, or 0 if correct. In the calling code, you can call this method in a loop until the return code == 0.
您可以通过返回int grom方法立即从该点返回,例如-1如果不正确并且您知道您需要重试,或者如果正确则返回0。在调用代码中,您可以在循环中调用此方法,直到返回代码== 0。
#3
1
I would do something like this:
我会做这样的事情:
Read the numbers in the method, no need to pass them as parameter. Read as them in a loop until both the number are inputted correctly then break. If wrong input then print the message and read again. For any wrong input Scanner.nextInt() will throw InputMismatchException
exception and there we will also consume the new line character from that input.
读取方法中的数字,不需要将它们作为参数传递。在循环中读取它们,直到两个数字都正确输入然后中断。如果输入错误,则打印信息并再次阅读。对于任何错误的输入,Scanner.nextInt()将抛出InputMismatchException异常,并且我们还将使用该输入中的新行字符。
public static int insertNumbers() {
int num1;
int num2;
while(true) { // loop until correct input.
System.out.println("Insert two numbers");
try {
num1 = numberScanner.nextInt();
num2 = numberScanner.nextInt();
break; // break once correct input is recorded.
} catch (InputMismatchException e) {
System.out.println("Equation format incorrect"); // print message and repeat again for input.
numberScanner.nextLine();
}
}
answer = num1 + num2;
return answer; // return the results.
}
#4
0
As an alternative to returning an exit code or using a loop (which is what I believe is more obvious because you can use continue
), you can call the function again and prevent the original instance from going further:
作为返回退出代码或使用循环的替代方法(我认为这可以更明显,因为您可以使用continue),您可以再次调用该函数并阻止原始实例进一步:
public int foo(){
//do stuff
if (must_restart){
return foo();
//or, if the function "returns" void: (note that 2 returns/having statements after a return is pointless, this is just an example)
foo();
return;
}
}
#1
1
This is what i propose:
这就是我的建议:
in the else case, you just use this construction
在else的情况下,你只需使用这种结构
else{
System.out.println("Equation format incorrect");
return insertNumbers();
}
Please be aware the code will keep on asking input until the input is two numbers. No need for the arguments in the method declaration, since you don't use them
请注意,代码将继续询问输入,直到输入为两个数字。不需要方法声明中的参数,因为您不使用它们
#2
1
You can return immediately from that point, by returning an int grom the method, eg -1 if not correct and you know you need to retry, or 0 if correct. In the calling code, you can call this method in a loop until the return code == 0.
您可以通过返回int grom方法立即从该点返回,例如-1如果不正确并且您知道您需要重试,或者如果正确则返回0。在调用代码中,您可以在循环中调用此方法,直到返回代码== 0。
#3
1
I would do something like this:
我会做这样的事情:
Read the numbers in the method, no need to pass them as parameter. Read as them in a loop until both the number are inputted correctly then break. If wrong input then print the message and read again. For any wrong input Scanner.nextInt() will throw InputMismatchException
exception and there we will also consume the new line character from that input.
读取方法中的数字,不需要将它们作为参数传递。在循环中读取它们,直到两个数字都正确输入然后中断。如果输入错误,则打印信息并再次阅读。对于任何错误的输入,Scanner.nextInt()将抛出InputMismatchException异常,并且我们还将使用该输入中的新行字符。
public static int insertNumbers() {
int num1;
int num2;
while(true) { // loop until correct input.
System.out.println("Insert two numbers");
try {
num1 = numberScanner.nextInt();
num2 = numberScanner.nextInt();
break; // break once correct input is recorded.
} catch (InputMismatchException e) {
System.out.println("Equation format incorrect"); // print message and repeat again for input.
numberScanner.nextLine();
}
}
answer = num1 + num2;
return answer; // return the results.
}
#4
0
As an alternative to returning an exit code or using a loop (which is what I believe is more obvious because you can use continue
), you can call the function again and prevent the original instance from going further:
作为返回退出代码或使用循环的替代方法(我认为这可以更明显,因为您可以使用continue),您可以再次调用该函数并阻止原始实例进一步:
public int foo(){
//do stuff
if (must_restart){
return foo();
//or, if the function "returns" void: (note that 2 returns/having statements after a return is pointless, this is just an example)
foo();
return;
}
}