gpt4 book ai didi

java - 无法找出错误 Luhn 检查

转载 作者:行者123 更新时间:2023-12-01 11:02:20 26 4
gpt4 key购买 nike

它应该使用 luhn 检查来告诉我一张卡是有效还是无效4388576018402626 无效4388576018410707 有效但它一直告诉我一切都是无效的:/任何关于做什么或去哪里寻找的建议都会很棒。我已经被困了几个小时了。如果人们告诉我有关如何查找代码未按预期工作的原因的任何提示,也会有所帮助。我使用 eclipse 和 java

public class Task11 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a credit card number as a long integer: ");
long number = input.nextLong();


if (isValid(number)) {
System.out.println(number + " is valid");
} else {
System.out.println(number + " is invalid");
}


}

public static boolean isValid(long number) {

return (getSize(number) >= 13) && (getSize(number) <= 16)
&& (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 6) || prefixMatched(number, 37))
&& (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0;
}

public static int sumOfDoubleEvenPlace(long number) {

int result = 0;

long start = 0;

String digits = Long.toString(number);

if ((digits.length() % 2) == 0) {
start = digits.length() - 1;
} else {
start = digits.length() - 2;
}


while (start != 0) {


result += (int) ((((start % 10) * 2) % 10) + (((start % 10) * 2) / 2));

start = start / 100;

}

return result;
}


public static int getDigit(int number) {

return number % 10 + (number / 10);

}

public static int sumOfOddPlace(long number) {

int result = 0;

while (number != 0) {

result += (int) (number % 10);

number = number / 100;

}

return result;

}

public static boolean prefixMatched(long number, int d) {

return getPrefix(number, getSize(d)) == d;

}

public static int getSize(long d) {

int numberOfDigits = 0;

String sizeString = Long.toString(d);
numberOfDigits = sizeString.length();

return numberOfDigits;

}


public static long getPrefix(long number, int k) {

String size = Long.toString(number);

if (size.length() <= k) {
return number;
} else {
return Long.parseLong(size.substring(0, k));
}
}
}

最佳答案

您应该修改您的isValid()当它不起作用时写下的方法,如下所示:

 public static boolean isValid(long number) {
System.err.println();
if(getSize(number) < 13){
System.out.println("Err: Number "+number+" is too short");
return false;
} else if (getSize(number) > 16){
public static boolean isValid(long number) {
System.err.println();
if(getSize(number) < 13){
System.out.println("Err: Number "+number+" is too short");
return false;
} else if (getSize(number) > 16){
System.out.println("Err: Number "+number+" is too long");
return false;
} else if (! (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 6) || prefixMatched(number, 37)) ){
System.out.println("Err: Number "+number+" prefix doesn't match");
return false;
} else if( (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 != 0){
System.out.println("Err: Number "+number+" doesn't have sum of odd and evens % 10. ");
return false;
}
return true;
}

我对你的问题的猜测是在getPrefix()上方法,你也应该在这里添加一些日志。

编辑:所以,有更多时间来帮助您(不知道是否仍然有必要,但无论如何)。另外,我纠正了我写的方法,有一些错误(例如, getSize(number) >= 13 的相反是 getSize(number) < 13 )...首先,使用一组数据进行测试会比每次自己输入值更快(添加您要检查的值):

public static void main(String[] args) {
long[] luhnCheckSet = {
0, // too short
1111111111111111111L, // too long (19)
222222222222222l // prefix doesn't match
4388576018402626l, // should work ?
};

//System.out.print("Enter a credit card number as a long integer: ");
//long number = input.nextLong();

for(long number : luhnCheckSet){
System.out.println("Checking number: "+number);
if (isValid(number)) {
System.out.println(number + " is valid");
} else {
System.out.println(number + " is invalid");
}
System.out.println("-");
}
}

我不知道这件事的细节,但我认为你应该与 String 合作一直以来,仅在需要时解析为 long (如果数字超过 19 个字符,它可能不会解析它 long )。尽管如此,还是做多。

我详细介绍了您的getPrefix()更多日志并放入 d in 参数 in long (小心比较原始类型是个好习惯):

public static boolean prefixMatched(long number, long d) {
int prefixSize = getSize(d);
long numberPrefix = getPrefix(number, prefixSize);
System.out.println("Testing prefix of size "+prefixSize+" from number: "+number+". Prefix is: "+numberPrefix+", should be:"+d+", are they equals ? "+(numberPrefix == d));
return numberPrefix == d;
}

仍然不知道这段代码有什么问题,但看起来它来自上次测试:我没有这样做,但你应该从 sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 中创建一种方法并记录数字和总和(就像我在 prefixMatched() 中所做的那样)。在这两种方法中添加日志,以确保它获得您想要的结果/正常工作。你用过调试器吗?如果可以的话,就这样做,它比添加大量日志要快!

祝你好运

关于java - 无法找出错误 Luhn 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33238963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com