简体   繁体   English

Java练习-检查ID号

[英]Java excercise - checking ID number

I have a task with checking ID number and I must check if this ID has 11 characters, if those characters are digits and I must check control number. 我有一个检查ID号的任务,我必须检查此ID是否包含11个字符,如果这些字符是数字,则必须检查控制号。 Number is correct when this equation is correct: 该方程正确时,数字是正确的:

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

Sample correct ID is: 49040501580 样本正确的ID为: 49040501580

And here is my program. 这是我的程序。 I don't know how to check if ID is digit and why it isn't correct. 我不知道如何检查ID是否为数字以及为什么它不正确。 Anyone help? 有人帮忙吗? XD Thank you in advance :3 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;
            }
        }
    }

}

Sample output 样品输出

With a minimal number of changes to your original code, I believe this is what you need. 只需对原始代码进行最少的更改,我相信这就是您所需要的。

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");
                    }
            }
    }

As you can see, we're now verifying the string length before the loop starts. 如您所见,我们现在在循环开始之前验证字符串长度。

Then we're checking each character is a digit one by one and quitting with a new error message if one of them is not. 然后,我们检查每个字符是否都是一个数字,如果其中一个不是,则退出并显示新的错误消息。 You'll need to provide the isDigit function yourself (plenty to choose from on StackOverflow!). 您需要自己提供isDigit函数(在StackOverflow上有很多选择!)。

We're accumulating the digit values multiplied by the weight into the amount variable, with a new value being added each time the loop iterates. 我们将数字值乘以权重后累加到数量变量中,每次循环时都会添加一个新值。

Finally we verify the result is correct after the loop has finished and all 11 characters have been processed. 最后,我们在循环完成并处理了所有11个字符后,验证结果是否正确。

NOTE: I don't normally work in Java so I'm not entirely sure if you can get the digit value out of a Character type object (to multiply it with the weight) like this. 注意:我通常不会在Java中工作,所以我不确定是否可以像这样从Character类型对象中获取数字值(将其乘以粗细)。 You might find that you are getting ASCII code values, or something else entirely. 您可能会发现自己正在获取ASCII码值,或者完全是其他值。 If this happens, you'll need to convert 'weights' into an array of integers, and extract the actual numeric value from the Character before multiplying them together. 如果发生这种情况,则需要将“权重”转换为整数数组,并从“字符”中提取实际数值,然后再将它们相乘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM