我正在尝试编写一个程序,从用户那里读取 2 个数字并将它们分开。这是我到目前为止的代码:
import java.util.Scanner;
public class divideByZero {
public static int quotient(int numerator, int denominator)
{
return numerator / denominator;
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the first number: ");
int numerator = scanner.nextInt();
System.out.print("Please enter the second number: ");
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
float result2 = (float)result;
System.out.printf("\n The first number %d divided by the second number "
+ "%d = %f\n", numerator, denominator, result2 );
}
如何将其更改为 while 循环,使用 if 语句检查第二个数字,如果为 0,请用户输入不同的数字?