我正在努力找出代码中的编译/语法错误。
public class CreditCardValidation {
public static void main (String[] args){
System.out.print(prefixMatched(4388576018402626, 4388));
}
/*
Return the number of digits in d
*/
public static int getSize(long d) {
int size = 0 ;
while( d > 0 ) {
d = d / 10 ;
size = size + 1 ;
}
return size ;
}
/*
Return the first k number of digits from number. If the number of digits in number is
less than k, return the number.
*/
public static long getPrefix(long n, int k) {
int f = getSize(n)-k;
long prefix = n/((int)(Math.pow(10, f)));
return prefix;
}
/*
Return true if the digit d is a prefix for number.
*/
public static boolean prefixMatched( long number, int d ) {
if ( d == getPrefix(number, 4))
return true ;
else
return false ;
}
}
正如你所看到的,我正在尝试调用 prefixMatched 来检查信用卡号是否符合要求;如果数字 d 是号码的前缀。然而,我从编译器得到的唯一结果是:
"CreditCardValidation.java:6: integer number too large: 4388576018402626
System.out.print(prefixMatched(4388576018402626, 4388));
^"
如果我的问题太浮夸,我很抱歉,这是我的第一篇文章。
最佳答案
- 您需要向编译器表明您的常量(CC 编号)是一个 long。在常量末尾添加 L。
- 将 CC 数字视为字符串并使用 charAt(x) 计算校验位实际上更容易一些。
关于java - 编译错误,整数太大。信用卡分析.Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19639640/