1

I have a code and I know that it isn't right. My task is "print all two-digit numbers which don't have two equal numbers". That means - program need to print numbers like 10, 12, 13 etc. Program didnt need to print 11 because there are 2 equal numbers. Hope that my program at least some is correct. (And sorry for my english).

public class k_darbs1 {

    public static void main(String[] args) {
        int a,b;
        boolean notequal;
        for(a = 10; a < 100; a++)
        {
            notequal = true;
            for(b = 100; b < a; b++)
            {
                if(a != b)
                {
                    notequal = false;
                }
            }
            if(notequal == true)
            {
                System.out.println(a);
            }
        }
    }
}
4

4 回答 4

7

so why making things to much complex!?!!?!!!??

public static void main(String[] args) {
        for(a = 10; a < 100; a++)
        {
            if(a%11==0){continue;}
            System.out.println(a);
        }
    }
于 2013-10-21T14:29:15.060 回答
3

I think your making this slightly more complicated than it has to be. If n is a 2-digit number, then the leading digit is n/10 (integer division by 10) and the trailing digit is n%10 (modulo 10). You can just test if those two are unequal and print n as appropriate, there's no need for another for-loop.

For instance:

int n = 42;

System.out.println(n/10);
System.out.println(n%10);
4
2
于 2013-10-21T14:28:31.903 回答
1

Convert it to a string and check the characters.

for (int a = 10; a < 100; a++) {
    String value = String.valueOf(a);

    if (value.charAt(0) != value.charAt(1)) {
        System.out.println(value);
    }
}
于 2013-10-21T14:30:59.980 回答
0

You can parse Integer to String. Then compare the numbers with substring. For this process, you need to know
Integer.toString(i);. string.substring(); methods. This is not a very efficent way but it is a solution.

于 2013-10-21T14:34:03.813 回答