我正在研究的问题是,
"写入是一个倍数,它确定一对整数是否 第二个整数是第一个整数的倍数。该方法需要 2 整数参数,如果秒是 首先,否则为假。[提示:使用余数运算符]。 将此方法合并到输入一系列 整数对(一次 1 对(并确定第二个 每对中的值是第一对的倍数。
现在,我编写了一个程序,它有点有效,但我似乎无法让它继续运行。当按下 0 时它应该停止,但它没有超过第一个整数。
当我改变 while (第一个 != 0(; 到 while (第一个 == 0(;时,我确实让它运行了一个周期;
我只是遗漏了什么还是完全错误了?任何信息都会有所帮助。
这是我的代码
import java.util.Scanner;
public class Multiples
{
public static void main(String[] args)
{
System.out.println("Len Rogers - Programming Assignment 4n");
Scanner input = new Scanner( System.in );
int first; // first number
int second; // second number
System.out.print( "Enter first number (0 to exit): " );
first = input.nextInt();
// set 0 as the sentinel value, since we cannot divide by 0
while (first != 0);
{
System.out.print("Enter second number: " );
second = input.nextInt();
if ( ismultiple(first, second ) )
System.out.printf( "truenn", second, first );
else
System.out.printf( "falsenn" , second, first );
System.out.print( "Enter first number (0 to exit): " );
first = input.nextInt();
} // end while loop
} // end main
// determin if the firest integer is a multiple of the second
public static boolean ismultiple( int firstNumber, int secondNumber )
{
return secondNumber % firstNumber == 0;
} // end method multiple
} // end class Multiples
我认为
do {
} while( first != 0 )
是您正在寻找的循环,因为它可以防止您需要单独的代码来读取第一个输入
如果您使用 readline,请检查您的读数是否可以转换为 int,否则退出您应该能够得到您想要的。