2024-08-14 01:26:08
我帮你改了一下,你看着是你要的吗?
首先import你没给出,这就不说了。其次checklong()方法不存在,你也没给出。我就粗略parse了一下,看到是long就返回真了。最大的问题是你这程序没有break,不会跳出而是一直要求输入而不输出,所以我在35行加了break。while(true)这种死循环要慎用……
其余的就是代码对齐缩进。
输出结果:
请输入身份证号码,以回车结束:
123456789123456
这是一个旧号码!
i=1 ai=0 wi=1
i=2 ai=6 wi=2
i=3 ai=5 wi=4
i=4 ai=4 wi=8
i=5 ai=3 wi=5
i=6 ai=2 wi=10
i=7 ai=1 wi=9
i=8 ai=9 wi=7
i=9 ai=8 wi=3
i=10 ai=7 wi=6
i=11 ai=9 wi=1
i=12 ai=1 wi=2
i=13 ai=6 wi=4
i=14 ai=5 wi=8
i=15 ai=4 wi=5
i=16 ai=3 wi=10
i=17 ai=2 wi=9
i=18 ai=1 wi=7
转化后的18位新号码为:12345619789123456X
代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.NumberFormat;
public class IDCard {
public static void main(String args[]){
try{
while(true){
System.out.println("请输入身份证号码,以回车结束:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
if(s.length()==15){
int total=0;
char f;
boolean t=checklong(s);
if(t){
System.out.println("这是一个旧号码!");
StringBuffer s1=new StringBuffer(s).insert(6,"19");
StringBuffer s2=new StringBuffer(s1).append("0");
for(int i=1;i<=18;i++){
char c=s2.charAt(18-i);
String s3=String.valueOf(c);
int ai=Integer.parseInt(s3);
double b=Math.pow(2,(i-1));
int wi=(int)(b)%11;
System.out.println("i="+i+'\t'+"ai="+ai+'\t'+"wi="+wi);
total+=ai*wi;
}
total=total%11;
char Check[]={'1','0','X','9','8','7','6','5','4','3','2'};
String str4=String.valueOf(Check[total]);
StringBuffer str5=new StringBuffer(s2).deleteCharAt(17);
StringBuffer str6=new StringBuffer(str5).append(str4);
System.out.println("转化后的18位新号码为:"+str6);
break;
} else {
System.out.println("你输入有误!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean checklong(String s) {
NumberFormat nf = NumberFormat.getNumberInstance();
try {
long l = nf.parse(s).longValue();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
2024-08-14 01:32:55
2024-08-14 00:00:00
2024-08-14 00:18:21
2024-08-14 03:07:44