【发布时间】:2013-10-28 16:06:02
【问题描述】:
我正在努力弄清楚我的代码中的编译/语法错误是什么。
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));
^"
如果我的问题过于笼统,我很抱歉,这是我的第一篇文章。
【问题讨论】:
-
你试过写4388576018402626L
-
值得补充的是,您使用数字来存储信用卡“号码”,您不会加减乘除信用卡号码,因此它不是数字,它是“单词”;所以将其存储为字符串
标签: java methods static-methods credit-card