第1个回答 2016-05-25
用了你说的方法写的
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] c = str.toCharArray();
if (Integer.parseInt(String.valueOf(c[0])) != 0) {
for (int i = 0; i < c.length; i++) {
try {
int a = Integer.parseInt(String.valueOf(c[i]));
int b = i;
if (b == c.length - 1) {
System.out.println("能转换");
}
} catch (Exception e) {
System.out.println("不能转换为有效数字");
}
}
} else {
System.out.println("不能转换为有效数字");
}
}
第2个回答 2016-05-25
try{
double a = Double.parseDouble("inputText");
}catch(Exception e){
System.out.println("输入的为非数字值");
}
可使用以上的方法,如果是数字,这此数字会被赋给 a ;如果出现异常,证明输入的不是数字本回答被网友采纳
第3个回答 2016-05-25
JAVA 从键盘输入一个字符串 判断其是否能被转换为?