简体   繁体   English

我正在创建的该程序必须检查一个数字是否为另一个数字的倍数

[英]This program that I am creating has to check to see if one number is a multiple of another number

This is what I have so far. 到目前为止,这就是我所拥有的。 If I put in a 4 for the first number and a 6 for the second it says that 4 is a multiple of 6,which is incorrect. 如果我在第一个数字中输入4,在第二个数字中输入6,则表示4是6的倍数,这是不正确的。 I am not sure what is going on with it. 我不确定这是怎么回事。

    System.out.print("\n Name =");
    String userName = console.readLine();

    System.out.print("\n First Number =");
    int firstNumber = console.readInt();

    System.out.print("\n Second Number =");
    int secondNumber = console.readInt();

    if (firstNumber / secondNumber == 0 || secondNumber / firstNumber == 0) {
        System.out.print("\n" + userName + "," + firstNumber + " is a multiple of " + secondNumber);
    }
    else {
        System.out.print("\n" + userName + "," + firstNumber + " is not a multiple of " + secondNumber);
    }

You can easily check if a number is a multiple of an other with a modulo. 您可以轻松地检查一个数是否是其他数与模的倍数。 Apply the module to the multiple you want to check, then simply confirm the remaining is equal to 0. 将模块应用于您要检查的倍数,然后简单地确认余数等于0。

Example : 范例:

int number = 3;
System.out.println("isMultiple : " + (number % 3 == 0));

As for what is wrong in your code, you divide and check if equals to 0, this is not the correct way to do. 至于代码中的错误,请划分并检查是否等于0,这不是正确的方法。

Look at this line 看这条线

firstNumber / secondNumber == 0

Replace firstNumber with 6 and secondNumber with 3, it will return 2 which won't be equals to 0 even if 3 is a multiple of 6. 将firstNumber替换为6,将secondNumber替换为3,它将返回2,即使3是6的倍数也将不等于0。

The simplest way to check if one number is a multiple of another is to use a modulo (remainder after division) operation. 检查一个数字是否是另一个数字的倍数的最简单方法是使用模(除法后的余数)运算。 So, the code might look like: 因此,代码可能类似于:

if(firstNumber % secondNumber == 0) {
    // I'm a multiple!
}

Remember to check that secondNumber is not zero, otherwise you'll run a into division by zero! 记住要检查secondNumber是否不为零,否则将被除以零!

Divide an int by an int give an int , so 6/4 == 0 . 将一个int除以一个int可得到一个int ,因此6/4 == 0

You can : 您可以 :

  • Use the module as suggested by JF Savard 使用JF Savard建议的模块
  • Cast first and second number into double or float , and check if the result is equals to itself casted as int (or rounded) 将第一个和第二个数字转换为doublefloat ,并检查结果是否等于转换为int (或舍入)的自身

What is wrong is this check: firstNumber / secondNumber == 0 and the other one there. 这是什么错误的检查: firstNumber / secondNumber == 0 ,而另一个在那。 This is not how you correctly check if a number divides another number. 这不是您正确检查数字是否除以另一个数字的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查一个数字是否等于另一个 - Check if one number is equal to another 如何编程检查数字是否为Zeisel数字? - How can I program to check whether a number is a zeisel number or not? 我正在创建一个Android应用,我希望多个人可以访问相同的号码,并能够通过两个按钮更改该号码 - I am creating an android app and I want multiple people to have access to the same number and be able to change that number with two buttons 如何检查多个动态数字等于另一个dyanami数字 - how to check multiple of a dynamic number to equal to another dyanami number 我正在做一个GUI检查编写器程序,我想问一下您如何使用用户键入的数字,并将其转换为JLabel中的单词? - I am doing a GUI check writer program, I want to ask how do you use the number that the user type, and convert it into words in a JLabel? 如何执行密码检查以查看是否有数字? - How do i implement a check on the password to see if there is a number? 我正在编写一个程序来查找给定数字的阶乘中 3 的数量。 来自用户的输入是数字 - I am writing a program to find number of 3's in factorial of a given number. The input from the user is the number 需要一个程序要求用户输入2个整数,检查第二个数字是否是第一个数字的倍数 - Need a programs that asks user to enter 2 integers, check to see if 2nd number is multiple of first number 在Java中,如何检查用户是否输入了另一个数字: - How To Check, In Java, If User Has Inputted Another Number: 我正在尝试检查电话号码格式是否正确 - I am trying to check if the phone number format is correct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM