升数字就是从左向右读,数值是依次上升的即可,比如123,1256,1389,
但是1123,165就不是。
以下是我的思路
public static void main(String[] args) {/*** 【请问1到10000之前,有多少升数字?】* 升数字就是从左向右读,数值是依次上升的即可,比如123,1256,1389,* 但是1123,165就不是。*/String strZs = null;Integer maxLngth = 0;int count = 0;//数量for(int i = 10;i<10000;i++) {//将int类型转成String类型strZs = String.valueOf(i);//定义该长度到数组String[] arr = new String[strZs.length()];//遍历将字符串赋值到数组中for(int j = 0;j<strZs.length();j++){arr[j] = strZs.substring(j, j+1);}boolean flag = isSort(arr);//如果是。打印,并将总数++if(flag){count++;System.out.println(i);}}System.out.println("数值是依次上升数量count="+count);}/*** 判断是不是生序数组* @param arr* @return*/private static boolean isSort(String[] arr) {for(int pxIndex =0;pxIndex<arr.length-1;pxIndex++) {// System.out.println(arr[pxIndex]);if (arr[pxIndex].compareTo(arr[pxIndex + 1]) > -1) {return false;}}return true;}
如有更好的解决方式,请随时沟通