public class Demo {
    public static void main(String[] args) throws Exception {
        String str = "1341241a4214141232" ;
        if (isNumber(str)) {
            System.out.println("是由数字所组成!");
        } else {
            System.out.println("不是由数字所组成!");
        }
    }
    public static boolean isNumber(String data) {
        char arr [] = data.toCharArray() ;  // 字符串变为字符数组
        for (int x = 0; x < arr.length; x++) {
            if (arr[x] < '0' || arr[x] > '9') {
                return false ;
            }
        }
        return true ;
    }
}这个结果判断的是:不是由数字所组成! 为什么if (isNumber(str)) 不写成if (isNumber(str)==true)

解决方案 »

  1.   

    isNumber(str)  默认返回true 了。再写就多此一举。
      

  2.   

    isNumber() 是java中的方法?  默认返回的是true?
      

  3.   

    if 语句检查一个 boolean 值。 isNumber()方法返回的已经是一个boolean 值。不需要再与true/false 进行比较。 如果觉得 if(true == true ){... } 也符合你的审美, 你那么写没什么问题。
      

  4.   

    isNumber() 是java中的方法?  默认返回的是true?是你自己写的方法啊 。你上面就有。
      

  5.   

    isNumber方法的实现用正则表达式实现比较合适    public static boolean isNumber(String data){
             String numRegEx = "^\\d+$";
     Pattern pattern = Pattern.compile(numRegEx);  
     Matcher m =pattern.matcher(data);
     return m.matches();
        }