簡體   English   中英

Java練習-檢查ID號

[英]Java excercise - checking ID number

我有一個檢查ID號的任務,我必須檢查此ID是否包含11個字符,如果這些字符是數字,則必須檢查控制號。 該方程正確時,數字是正確的:

ID = abcdefghijk

(1*a+3*b+7*c+9*d+1*e+3*f+7*g+9*h+1*i+3*j+1*k) % 10 = 0

樣本正確的ID為: 49040501580

這是我的程序。 我不知道如何檢查ID是否為數字以及為什么它不正確。 有人幫忙嗎? XD預先感謝您:3

import java.util.*;

public class wat {

    public static void main(String[] args) {
        char[] weights = {1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1};
        System.out.print("Enter next digits your ID number: ");
        Scanner keyboard = new Scanner(System.in);
        String number = keyboard.nextLine();
        char[] ofm = number.toCharArray();
        Character[] id = new Character[ofm.length];
        for (int i = 0; i < ofm.length; i++) {
            id[i] = ofm[i];
            System.out.print(id[i] + " ");
            int length = id.length;
            if (length == 11) {
                System.out.println("This ID number has 11 digits");
                System.out.println("Checking of the control number");
                int amount = 0;
                amount = id[i] * weights[i];
                System.out.println(amount);
                int result = 0;
                result = amount % 10;
                if (result == 0) {
                    System.out.println("ID number is correct");
                } else {
                    System.out.println("ID number is not correct");
                    break;
                }
            } else {
                System.out.print("This ID number hasn't 11 digits.");
                break;
            }
        }
    }

}

樣品輸出

只需對原始代碼進行最少的更改,我相信這就是您所需要的。

import java.util.*;
     public class wat {
            public static void main(String[] args) {
                    char[] weights = {1,3,7,9,1,3,7,9,1,3,1};
                    System.out.print("Enter next digits your ID number: ");
                    Scanner keyboard = new Scanner(System.in);
                    String number = keyboard.nextLine();
                    char[] ofm=number.toCharArray();
                    Character[] id=new Character[ofm.length];
                    if (ofm.length == 11) {
                        System.out.println("This ID number has 11 characters");
                        int amount = 0;
                        for (int i = 0; i < ofm.length; i++) {
                                id[i]=ofm[i];
                                System.out.print(id[i]+" ");
                                int length = id.length;
                                if (isDigit(id[i])) {
                                        amount = id[i]*weights[i];
                                } else {
                                    System.out.println("character is not a digit");
                                    break;
                                }
                        }
                    } else {
                        System.out.print("This ID number hasn't 11 digits.");
                        return;
                    }
                    System.out.println("Checking of the control number");
                    System.out.println(amount);
                    int result =0;
                    result = amount % 10;
                    if (result == 0) {
                        System.out.println("ID number is correct");
                    } else {
                        System.out.println("ID number is not correct");
                    }
            }
    }

如您所見,我們現在在循環開始之前驗證字符串長度。

然后,我們檢查每個字符是否都是一個數字,如果其中一個不是,則退出並顯示新的錯誤消息。 您需要自己提供isDigit函數(在StackOverflow上有很多選擇!)。

我們將數字值乘以權重后累加到數量變量中,每次循環時都會添加一個新值。

最后,我們在循環完成並處理了所有11個字符后,驗證結果是否正確。

注意:我通常不會在Java中工作,所以我不確定是否可以像這樣從Character類型對象中獲取數字值(將其乘以粗細)。 您可能會發現自己正在獲取ASCII碼值,或者完全是其他值。 如果發生這種情況,則需要將“權重”轉換為整數數組,並從“字符”中提取實際數值,然后再將它們相乘。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM