求一个验证数字或字母是否为升序或者降序!如123456,765432,abcdef等等!

解决方案 »

  1.   

    大致思路是:循环按char取出字符,然后直接比较其数值(ASCII),只要一直都更大就行了。String str = "1234560";
    for (int i=1; i<str.length(); i++) {
      if (str.charAt(i) < str.charAt(i-1)) {
         System.out.println("并非升序字符串");
      } 
    }
      

  2.   


    var text = "abcdefg"; //换成任意序列的数字或字母if(text.charCodeAt(0)>text.charCodeAt(1)){
    alert('降序');
    }else{
    alert('升序');
    }
      

  3.   

    charAt()串的每个值,比较就是了。