java - 卢恩。数字长度控制

标签 java luhn

我对此真的很陌生。我正在尝试使用 Luhn 算法来验证“人员编号”(瑞典社会安全号码)。我认为代码即将完成,但我不知道要在“birthDate.length()”和“pos”类中放入什么才能使其工作。 birthDate.length需要确保字符串长度为10位,并且需要“pos”进行验证控制。

package kund;
import java.util.Scanner;

public class Kund {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Welcome customer. Please login by using your "
                + "birthdate (yymmddxxxx)");
        Scanner input = new Scanner (System.in);
        String birthDate = input.next();

        int length = birthDate.length();
        int sum = 0;

        for (int i = 1; pos=length-1; i<=10; i++, pos--){
            char tmp = birthDate.charAt(pos);
            int num = Integer.parseInt(String.valueOf(tmp));

            int produkt;

            if (i % 2 != 0){
                produkt = num * 1;
            }else {
                produkt = num * 2;
            }
            if ( produkt > 9 )
                produkt -= 9;
            sum += produkt;

            boolean korrekt = (sum % 10) == 0;

            if (!korrekt){
                System.out.println("Invalid.");

            }else if(korrekt){
                System.out.println("Correct");
            }
        }
    }

}

最佳答案

您的代码语法不正确。您的 for 循环在循环头中有 4 个组件,而不是 3 个。我移动了 pos=length-1;事物到语法正确的位置。我不知道该算法应该如何工作,因此我无法检查该程序现在在语义上是否正确。

但是,我假设您有瑞典社会安全号码并且可以轻松查看。

package kund;

import java.util.Scanner;

public class Kund {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Welcome customer. Please login by using your "
                + "birthdate (yymmddxxxx)");
        Scanner input = new Scanner (System.in);
        String birthDate = input.next();

        int length = birthDate.length();
        int sum = 0;
        int pos = length-1;
        for (int i = 1; i<=10; i++, pos--){
            char tmp = birthDate.charAt(pos);
            int num = Integer.parseInt(String.valueOf(tmp));

            int produkt;

            if (i % 2 != 0){
                produkt = num * 1;
            }else {
                produkt = num * 2;
            }
            if ( produkt > 9 )
                produkt -= 9;
            sum += produkt;

            boolean korrekt = (sum % 10) == 0;

            if (!korrekt){
                System.out.println("Invalid.");
            }else if(korrekt){
                System.out.println("Correct");
            }
        }
    }
}

关于java - 卢恩。数字长度控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26335509/

相关文章:

java - 需要什么才能将 JPanel 和 JButton 转换为与 IOS 一起使用,以避免 “headless” 错误?

java - JNA:在 Spotify API 中找不到 C 方法

java - 如何为每个 RDD Spark Streaming

dataframe - 如何在 Spark Scala 中检查 df 列中的 Luhn

javascript - 如何计算字符串中的某些字符

java - Errors/BindingResult 参数应在模型属性、@RequestBody 或 @RequestPart 参数之后立即声明

java - INSTALL_PARSE_FAILED_NO_CERTIFICATES 安装修改后的 apk 文件失败(无效的 SHA1 签名),即使在使用 jarsigner 签名之后

java - 使用 Luhn 算法检查信用卡有效性

Paypal 专业版 : is credit card validation really necessary?