你需要类似的东西吗:
import java.util.Scanner;
public class Divide {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
double ratio;
Scanner scan = new Scanner(System.in);
int x = 1; // using variable for do-while loop
do { // re-runs code if input error
try {
System.out.println("Let's divide some numbers!");
System.out.print("Enter your first number: ");
num1 = scan.nextInt();
System.out.print("Enter your second number: ");
num2 = scan.nextInt();
if (num2 != 0) {
ratio = ((double) num1 / num2);
System.out.println("Eureka! the answer is " + ratio);
} else {
System.out.println("The second number should not be 0");
}
} catch (Exception e) {
System.out.println("This won't work. Try again.");
x = 2;
}
} while (x == 1);
}
}
示例运行:
Let's divide some numbers!
Enter your first number: 10
Enter your second number: 5
Eureka! the answer is 2.0
Let's divide some numbers!
Enter your first number: 12
Enter your second number: 0
The second number should not be 0
Let's divide some numbers!
Enter your first number: 23
Enter your second number: 5
Eureka! the answer is 4.6
Let's divide some numbers!
Enter your first number: 50
Enter your second number: abc
This won't work. Try again.
此时程序将停止执行。
更新:我刚刚看到您的评论,即使输入非法(例如 abc),您也不希望您的程序停止。为此,您需要做的就是再次实例化Scanner 对象。下面给出的是完整的示例运行程序:
import java.util.Scanner;
public class Divide {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
double ratio;
Scanner scan = new Scanner(System.in);
int x = 1; // using variable for do-while loop
do { // re-runs code if input error
try {
System.out.println("Let's divide some numbers!");
System.out.print("Enter your first number: ");
num1 = scan.nextInt();
System.out.print("Enter your second number: ");
num2 = scan.nextInt();
if (num2 != 0) {
ratio = ((double) num1 / num2);
System.out.println("Eureka! the answer is " + ratio);
} else {
System.out.println("The second number should not be 0");
}
} catch (Exception e) {
System.out.println("This won't work. Try again.");
scan = new Scanner(System.in);
}
} while (x == 1);
}
}
示例运行:
Let's divide some numbers!
Enter your first number: 10
Enter your second number: 5
Eureka! the answer is 2.0
Let's divide some numbers!
Enter your first number: 10
Enter your second number: 0
The second number should not be 0
Let's divide some numbers!
Enter your first number: 10
Enter your second number: abc
This won't work. Try again.
Let's divide some numbers!
Enter your first number: 10
Enter your second number: 5
Eureka! the answer is 2.0
Let's divide some numbers!
Enter your first number:
我希望,这可以满足您的要求。如果您还需要其他东西,请随时告诉我。
现在,您甚至可以删除冗余变量x,如下所示:
import java.util.Scanner;
public class Divide {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
double ratio;
Scanner scan = new Scanner(System.in);
while (true) {
try {
System.out.println("Let's divide some numbers!");
System.out.print("Enter your first number: ");
num1 = scan.nextInt();
System.out.print("Enter your second number: ");
num2 = scan.nextInt();
if (num2 != 0) {
ratio = ((double) num1 / num2);
System.out.println("Eureka! the answer is " + ratio);
} else {
System.out.println("The second number should not be 0");
}
} catch (Exception e) {
System.out.println("This won't work. Try again.");
// re-runs code if input error
scan = new Scanner(System.in);
}
}
}
}