简体   繁体   English

如何在Java中比较INT值中的数字?

[英]How to compare numbers in an INT value in Java?

I'm writing a code for an assignment for school but it requires me to compare two int values. 我正在为学校的作业编写代码,但这需要我比较两个int值。 For example, if you have the number 123 and the other number is 321, they have the same digits but are in different orders. 例如,如果您有数字123,而另一个数字是321,则它们具有相同的数字,但顺序不同。 Is there easy way of comparing them or do i have to make them into a string and compare them as string types? 有没有比较它们的简便方法,还是我必须将它们制成字符串并将它们作为字符串类型进行比较? if its the latter, how could i compare two strings? 如果是后者,我如何比较两个字符串? Is there any way of doing this without an array? 没有数组有没有办法做到这一点?

By comparing int values, if you mean greater than, less than or equal you can do that like so. 通过比较int值,如果您表示大于,小于或等于,则可以这样做。

int a = 123, b= 321;

if(a > b) 
//a is greater than b (b is less than a)

if(a == b)
// a is equal to b

if(a < b)
// a is less than b (b is greater)

Could use some clarification, if you want to check if the number is reversed like you said in an example its called a palindrome. 如果您想检查数字是否相反(如您在示例中所说的称为回文) ,可以进行一些说明

You could reverse a number in the following if you had experience with loops and modulo(the %) in the following snippet. 如果您对以下代码段中的循环和取模(%)有经验,可以在下面反转一个数字。

int r = 0;
while(number != 0){
 r = r * 10 + number % 10;
 number /= 10;  }
 return r;

r would be that number reversed. r将是相反的数字。 If you input let's say 123 you would get 321 back, then you could compare it to the other to see if its just the reverse. 如果输入123,您将得到321,那么您可以将其与另一个进行比较以查看其是否恰好相反。

Let me know if you have any more questions and I'll try to answer! 让我知道您是否还有其他问题,我会尽力解答!

To check if a number is arbitrarily mixed and not reversed to winning number, you could try the following. 要检查数字是否是任意混合的而不是倒数为中奖数字,可以尝试以下方法。

Two numbers a and b, a is the winning number and b is the number the user chose. 两个数字a和b,a是中奖号码,b是用户选择的号码。 a is 251 and b is 521. a是251,b是521。

You could do this on each number to separate them. 您可以对每个数字进行分隔。

int p1,p2,p3;
p1 = num % 10;
p2 = num / 10 % 10;
p3 = num / 100 % 10;

This would separate ex. 这将分开。 251 into 2, 5, and then 1. Then you could add them as so doing the same process for the second. 251分别放入2、5和1。然后您可以添加它们,第二步相同。 sum is p1 + p2 + p3 and sum2 is p4 + p5 + p6 for the second number. 对于第二个数,sum是p1 + p2 + p3,sum2是p4 + p5 + p6。 Provided the numbers are not reversed. 前提是数字不取反。 Use the thing I mentioned before for that case to check if they are flipped. 在这种情况下,请使用我之前提到的东西检查它们是否被翻转。

if(sum == sum2)
//Numbers are mixed but you won!

This should work. 这应该工作。

Certainly not the fastest solution, but the code is short and easy to understand. 当然,这不是最快的解决方案,但是代码简短易懂。

public boolean arePalindromes(int a, int b){
    //convert them to char arrays for easy sorting
    char[] aryA = String.valueOf(a).toCharArray();
    char[] aryB = String.valueOf(b).toCharArray();

    //sort them
    Collections.sort(aryA);
    Collections.sort(aryB);

    //put them back to strings for easy comparison
    String strA = new String(aryA);
    String strB = new String(aryB);

    //compare
    return strA.equals(strB);
}

Please try the following code (I have tested it), of which idea is borrowed from here and here . 请尝试以下代码(我已经对其进行了测试),该代码的思想是从这里这里借来的。 This solution still uses array. 此解决方案仍使用数组。

public class CompareInt {
    public static void main(String[] args) {
        System.out.println(containSameDigits(123, 123));
        System.out.println(containSameDigits(123, 321));
        System.out.println(containSameDigits(123, 132));
        System.out.println(containSameDigits(123, 323));
        System.out.println(containSameDigits(123, 124));
        System.out.println(containSameDigits(123, 111));
    }

    public static boolean containSameDigits(int x, int y) {
        String xSorted = getSortedString(x);
        String ySorted = getSortedString(y);
        return xSorted.equalsIgnoreCase(ySorted);
    }

    public static String getSortedString(int x) {
        String xSorted = "";
        for (int digit = 0; digit < 9; digit++) {
            for (int temp = x; temp > 0; temp /= 10) {
                if (temp % 10 == digit) {
                    xSorted += digit;
                }
            }
        }
        return xSorted;
    }
}

Output: 输出:

true
true
true
false
false
false

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM