Java 第一个数字是第二个数字的倍数吗
我正在编写一个程序,要求用户输入两个整数,总共十次 接下来,程序需要评估第一个整数是否是第二个整数的倍数 如果第一个是第二个的倍数,则程序应打印为真,如果不是,则应打印为假 这是我的密码:Java 第一个数字是第二个数字的倍数吗,java,Java,我正在编写一个程序,要求用户输入两个整数,总共十次 接下来,程序需要评估第一个整数是否是第二个整数的倍数 如果第一个是第二个的倍数,则程序应打印为真,如果不是,则应打印为假 这是我的密码: public static void main(String[] args) { Scanner input = new Scanner(System.in); int counter = 0; // Initializes the counter
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0; // Initializes the counter
System.out.printf("Enter first integer: "); // asks the user for the first integer
int number1 = input.nextInt(); // stores users input for the first integer
System.out.printf("Enter second integer: "); // asks the user for the second integer
int number2 = input.nextInt(); // stores the users input for the second integer
while (counter <= 10) // starts a loop that goes to 10
{
if (number1 & number2 == 0) // checks to see if number1 is a multiple of number2
System.out.print("true"); // if so then print out "true"
else
System.out.print("false"); // otherwise print out "false"
}
} // end class
我的代码在某个地方被破解了。是否有人可以帮助我,或者至少为我指出正确的方向?&是按位逻辑AND函数。我很确定那不会做你想让它做的事。似乎你想要模运算符,%
例如,使用数字1%数字2。而不是1号和2号
while (counter <= 10) // starts a loop that goes to 10
{
if (number1 % number2 == 0) // checks to see if number1 is a multiple of number2
System.out.print("true"); // if so then print out "true"
else
System.out.print("false"); // otherwise print out "false"
}
您需要将两个输入读取10次。并测试number1是否是number2的倍数。差不多
public static void main(String str[]) throws IOException {
Scanner input = new Scanner(System.in);
for (int counter = 0; counter < 10; counter++) {
System.out.printf("Enter first integer for counter %d: ", counter);
int number1 = input.nextInt();
System.out.printf("Enter second integer for counter %d: ", counter);
int number2 = input.nextInt();
// Since you want to print true if number1 is a multiple of number2.
System.out.println(number1 % number2 == 0);
}
}
使用%而不是&来检查number1是否是number2的倍数;然而,现在我需要程序循环十次并打印出真或假。