If语句检查数字的最后一位
If Statement to check last digit of number
我想通过 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
returns newNumber2
的最后一位。 a % b
是 a
除以 b
的余数 - 所以这是 newNumber2
除以 10 的余数,恰好是最后一位。
所以你可以使用,例如:
if((newNumber2 % 10) == 1)
(额外的括号是一种风格偏好,不是必需的)
另一种方法(不如模数好)是在数字上使用 .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];
我想通过 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
returns newNumber2
的最后一位。 a % b
是 a
除以 b
的余数 - 所以这是 newNumber2
除以 10 的余数,恰好是最后一位。
所以你可以使用,例如:
if((newNumber2 % 10) == 1)
(额外的括号是一种风格偏好,不是必需的)
另一种方法(不如模数好)是在数字上使用 .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];