每段都要註解
import java.util.Scanner;
public class IDCerti {
public static void main(String[ ] args){
Scanner scn=new Scanner(System.in);
String id;
boolean b;
do{
System.out.print("輸入身分證字號:");
id=scn.next();
id=id.toUpperCase();
b=idCheck(id);
}while(!b);
}
public static boolean idCheck(String id){
if(id.length()!=10){
System.out.println("身分證字數 NG");
return false;
}
if( ('A'>id.charAt(0)) || (id.charAt(0)>'Z' ) ){
System.out.print("第一個字母不為英文字母");
return false;
}
if ( ('1'!= id.charAt(1))&&(id.charAt(1)!='2' ) ){
System.out.print("第二個字不為1或2");
return false;
}
//================================================
for(int i=2;i<10;i++){
if( ('0'>id.charAt(i)) || (id.charAt(i)>'6' ) ){
System.out.print("第"+(i+1)+"個字不為數字");
}
}
String chkid="ABCDEFGHJKLMNPQRSTUVXYWZIO";
char c=id.charAt(0);
int checkNumber=chkid.indexOf(c)+10;
//System.out.print("轉換數值:"+checkNumber);
//System.out.print(id.substring(1));
String ss=checkNumber+id.substring(1);
//System.out.print(ss);
int sum=(ss.charAt(0)-'0')*1;
for(int i=1;i<=9;i++)
sum+=(ss.charAt(i)-'0')*(10-i);
sum+=(ss.charAt(10)-'0')*1;
if(sum%10!=0){
System.out.print("不是10的倍數"+sum);
return false;
}
else{
System.out.print(id+"身分證字號正確");
return false;
}
}
}
================================================================
public class Method2 {
public static void main(String[] args) {
int tot1, tot2;
tot1 = sum(1, 10);
System.out.println("1加到10的總和為" + tot1 + "\n");
tot2 = sum(5, 12);
System.out.println("5加到12的總和為" + tot2 + "\n");
}
static int sum(int vStart, int vEnd) {
int total = 0;
for (int i = vStart; i <= vEnd; i++)
total += i;
return total;
}
}
拜託萬能的閒版了!!