问题描述
- JAVA连位数字判断如,1234 ,7890,8901,9012求解决
- 如题,求高手解答在线等,我现在只实现了,1234这种的不能实现8901这种的,
public static boolean isDescNumeric(String numOrStr) {
boolean flag = true;
for (int i = 0; i < numOrStr.length(); i++) {
if (i > 0) {
int num = Integer.parseInt(numOrStr.charAt(i) + """");
int num_ = Integer.parseInt(numOrStr.charAt(i - 1) + """") - 1;
if (num != num_) {
flag = false;
break;
}
}
}
return flag;
}
解决方案
http://ideone.com/9HehGv
在线运行通过
解决方案二:
/* package whatever; // don't place package name! */import java.util.*;import java.lang.*;import java.io.*;/* Name of the class has to be ""Main"" only if the class is public. */class Ideone{ public static boolean isDescNumeric(String numOrStr) { int start = (int)(numOrStr.charAt(0) - '0'); for (int i = 0; i < numOrStr.length(); i++) { int n = (int)(numOrStr.charAt(i) - '0'); if (n != (start + i) % 10) return false; } return true; } public static void main (String[] args) throws java.lang.Exception { // your code goes here System.out.println(isDescNumeric(""1234"")); System.out.println(isDescNumeric(""8901"")); System.out.println(isDescNumeric(""1345"")); }}
true
true
false
解决方案三:
所有输入都是false
你这是想要做什么判断?
解决方案四:
public static boolean isDescNumeric(String numOrStr) { boolean flag = true; for (int i = 0; i < numOrStr.length() -2; i++) { int num = Integer.parseInt(numOrStr.charAt(i) + """") + 1; int num_ = Integer.parseInt(numOrStr.charAt(i + 1) + """") ; if (num > 9){ num = 1; } if (num != num_ ) { flag = false; break; } }return flag;}
解决方案五:
就只有一种特殊情况,9到0
public static boolean isDescNumeric(String numOrStr) {
boolean flag = true;
for (int i = 0; i < numOrStr.length(); i++) {
if (i > 0) {
int num = Integer.parseInt(numOrStr.charAt(i) + """");
int num_ = Integer.parseInt(numOrStr.charAt(i - 1) + """") - 1;
if(num==9&&num_0==-1)
continue;//结束本次循环
if (num != num_) {
flag = false;
break;
}
}
}
return flag;
}
解决方案六:
public static boolean isDescNumeric(String numOrStr) { int start = (int)(numOrStr.charAt(0) + '0'); for (int i = 0; i < numOrStr.length(); i++) { int n = (int)(numOrStr.charAt(i) + '0'); if (n != (start + i) % 10) return false; } return true;}
解决方案七:
我感觉应该这样吧,和楼上的大同小异,思路都差不多
public static boolean isDescNumeric(String numOrStr){ //初始化标志位,默认所有的字符串都是连位的 boolean isDesc = true; //将字符串转为数组,方便取用 char[] numArr = numOrStr.toCharArray(); //循环数组,判断是否为连位 for(int i=0;i<numArr.length-1;i++){ //当前字符位置 int number = Integer.parseInt(numArr[i]); //数字当前位置的下一个 int numberNext = Integer.parseInt(numArr[i+1]); //开始判断是否为连位 int next = numberNext-1; //若当前位置为0,那么让它减去1后等于9 if( next == -1){ next = 9; } if(number != next){ isDesc = false; break; } } return isDesc; }
解决方案八:
不想写循环,换种思路玩玩。用空间来换,入参长度很大就要小心内存了。
public class Test { public static boolean isDescNumeric(String numOrStr){ String template = ""0123456789""; int size = (numOrStr.length() / template.length()) + 2; template = new String(new char[size]).replace("""" template); return template.contains(numOrStr); } public static void main(String[] args) { System.out.println(isDescNumeric(""1234"")); System.out.println(isDescNumeric(""8901"")); System.out.println(isDescNumeric(""1345"")); }}
时间: 2024-11-08 18:59:25