该方法应该检查输入的数字是否是数字而不是字母,然后完成功能。但是,每次我尝试它总是说输入错误。
例如:
如果我输入“5 * 5”
它说 Error Incorrect Input Re-input your numbers:
当它不应该做任何事情时。我想不通。
我的代码是:
public void multiply() throws IOException {
boolean valid = true;
boolean valid1 = true;
int i = 0;
int k = 0;
String string = input;
String[] strings = string.split("\\*");
String string1 = strings[0];
String string2 = strings[1];
for (valid = true; i < strings[0].length();) {
if (strings[0].charAt(i) == '0'
|| strings[0].charAt(i) == '1'
|| strings[0].charAt(i) == '2'
|| strings[0].charAt(i) == '3'
|| strings[0].charAt(i) == '4'
|| strings[0].charAt(i) == '5'
|| strings[0].charAt(i) == '6'
|| strings[0].charAt(i) == '7'
|| strings[0].charAt(i) == '8'
|| strings[0].charAt(i) == '9'
|| strings[0].charAt(i) == '*') {
valid = true;
} else {
valid = false;
}
i++;
}
for (valid1 = true; k < strings[1].length();) {
if (strings[1].charAt(k) == '0'
|| strings[1].charAt(k) == '1'
|| strings[1].charAt(k) == '2'
|| strings[1].charAt(k) == '3'
|| strings[1].charAt(k) == '4'
|| strings[1].charAt(k) == '5'
|| strings[1].charAt(k) == '6'
|| strings[1].charAt(k) == '7'
|| strings[1].charAt(k) == '8'
|| strings[1].charAt(k) == '9'
|| strings[1].charAt(k) == '*') {
valid1 = true;
} else {
valid1 = false;
}
k++;
if (valid1 == true && valid == true) {
System.out.println("Multiply");
//Going to finish
} else {
System.out.println("Error Incorrect Input");
System.out.println("Re-input your numbers:");
check();
}
}
}