import java.util.*;
class Dis {
static boolean Digitinstring(String s) {
boolean result = false;
int i, j;
char[] ch = s.toCharArray();
int x = ch.length;
for (j = 0; j < x; j++) {
for (i = 0; i <= 9; i++) {
if (ch[j] == i) {
System.out.println("True");
result = true;
} else {
result = false;
}
}
}
return result;
}
public static void main(String args[]) {
System.out.println("Enter the string");
Scanner ob = new Scanner(System.in);
String s = ob.nextLine();
System.out.println(Digitinstring(s));
}
}
此代码始终给出答案false
。 if条件不起作用。 我能做些什么才能使它正常工作?如何判断字符串中是否有数字?
删除'else'部分。 'ch [j]'包含'char',但'i'是'int',''2'== 2'永远不会是真的。 – Pshemo
你只想知道是否有字符串中的数字或不正确的,那么它很简单,你不需要编写嵌套循环我会在几分钟内上传一个相同的工作程序 –