如果声明检查数字的最后一位数

时间:2015-03-22 22:35:04

标签: java swing if-statement numbers

我想通过if语句查看结束号码。

例如

if (number ends in 1)

    tNumber.setText("1");

目前我的代码可以正常运行,但是当我为更大的数字构建更大的代码时,它会非常耗时。

到目前为止,这是我的代码:

{
    timer2 = new Timer(100, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            int newNumber = (int) (Math.random() * 10);
            tNumber4.setText("" + newNumber);
            index2++;
            if (index2 >= 10 ) {
                ((Timer) e.getSource()).stop();
                String s = tTo.getText();
                String t = tFrom.getText();
                int x = Integer.parseInt(t);
                int d = Integer.parseInt(s);
                int newNumber2 = (int) (Math.random() * (d + 1 - x) + x);
                if (newNumber2 == 10 || newNumber2 == 20 || newNumber2 == 30
                        || newNumber2 == 40 || newNumber2 == 50 || newNumber2 == 60
                        || newNumber2 == 70 || newNumber2 == 80 || newNumber2 == 90){
                    tNumber4.setText("0");
                }else if (newNumber2 == 11 || newNumber2 == 21 || newNumber2 == 31
                        || newNumber2 == 41 || newNumber2 == 51 || newNumber2 == 61
                        || newNumber2 == 71 || newNumber2 == 81 || newNumber2 == 91){
                    tNumber4.setText("1");
                }else if (newNumber2 == 12 || newNumber2 == 22 || newNumber2 == 32
                        || newNumber2 == 42 || newNumber2 == 52 || newNumber2 == 62
                        || newNumber2 == 72 || newNumber2 == 82 || newNumber2 == 92){
                    tNumber4.setText("2");
                }else if (newNumber2 == 13 || newNumber2 == 23 || newNumber2 == 33
                        || newNumber2 == 43 || newNumber2 == 53 || newNumber2 == 63
                        || newNumber2 == 73 || newNumber2 == 83 || newNumber2 == 93){
                    tNumber4.setText("3");
                }else if (newNumber2 == 14 || newNumber2 == 24 || newNumber2 == 34
                        || newNumber2 == 44 || newNumber2 == 54 || newNumber2 == 64
                        || newNumber2 == 74 || newNumber2 == 84 || newNumber2 == 94){
                    tNumber4.setText("4");
                }else if (newNumber2 == 15 || newNumber2 == 25 || newNumber2 == 35
                        || newNumber2 == 45 || newNumber2 == 55 || newNumber2 == 65
                        || newNumber2 == 75 || newNumber2 == 85 || newNumber2 == 95){
                    tNumber4.setText("5");
                }else if (newNumber2 == 16 || newNumber2 == 26 || newNumber2 == 36
                        || newNumber2 == 46 || newNumber2 == 56 || newNumber2 == 66
                        || newNumber2 == 76 || newNumber2 == 86 || newNumber2 == 96){
                    tNumber4.setText("6" );
                }else if (newNumber2 == 17 || newNumber2 == 27 || newNumber2 == 37
                        || newNumber2 == 47 || newNumber2 == 57 || newNumber2 == 67
                        || newNumber2 == 77 || newNumber2 == 87 || newNumber2 == 97){
                    tNumber4.setText("7" );
                }else if (newNumber2 == 18 || newNumber2 == 28 || newNumber2 == 38
                        || newNumber2 == 48 || newNumber2 == 58 || newNumber2 == 68
                        || newNumber2 == 78 || newNumber2 == 88 || newNumber2 == 98){
                    tNumber4.setText("8" );
                }else if (newNumber2 == 19 || newNumber2 == 29 || newNumber2 == 39
                        || newNumber2 == 49 || newNumber2 == 59 || newNumber2 == 69
                        || newNumber2 == 79 || newNumber2 == 89 || newNumber2 == 99){           
                    tNumber4.setText("9");
                }
            }
        }
    });
    timer2.setInitialDelay(0);

必须有一个比这更简单的方法:

if (newNumber2 == 10 || newNumber2 == 20 || newNumber2 == 30
                        || newNumber2 == 40 || newNumber2 == 50 || newNumber2 == 60
                        || newNumber2 == 70 || newNumber2 == 80 || newNumber2 == 90){

我希望有类似的东西:

If (newNumber2 == *1){
tNumber4.setText("1")
}

我已经看过了视线,但我不确定如何说出我正在寻找的东西,这样就很难。

如果有人可以提供帮助,我们将不胜感激。

2 个答案:

答案 0 :(得分:5)

newNumber2 % 10返回newNumber2的最后一位数字。 a % ba的余数除以b - 所以这是newNumber2的余数除以10,恰好是最后一位数。

所以你可以使用,例如:

if((newNumber2 % 10) == 1)

(额外的括号是样式偏好,并且不是必需的)

答案 1 :(得分:0)

另一种方法(不像模数那样好)就是在数字上使用.toString()方法,然后对你想要的数字进行子串

所以它看起来像这样

Integer number = 12345;
String[] places = ArrayUtils.reverse(number.toString().split(""));
// Then you address the numbers by getting the nth index
// So the last number is the 0th index (places[0])
// Don't forget to parse the character back to an integer before doing any calculations on it
String lastNumber = places[0];