我想通过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")
}
我通过视线已经看到了,但是我不确定该如何表达我正在寻找的内容,因此很难。
如果有人可以帮助,将不胜感激。
最佳答案
newNumber2 % 10
返回newNumber2
的最后一位。 a % b
是a
的余数除以b
-因此,这是newNumber2
的余数除以10,恰好是最后一位。
因此,您可以使用例如:
if((newNumber2 % 10) == 1)
(多余的括号是样式的首选项,不是必需的)