java - 编译错误,整数太大。信用卡分析.Java

标签 java methods static-methods credit-card

我正在努力找出代码中的编译/语法错误。

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


                               ^"

如果我的问题太浮夸,我很抱歉,这是我的第一篇文章。

最佳答案

  1. 您需要向编译器表明您的常量(CC 编号)是一个 long。在常量末尾添加 L
  2. 将 CC 数字视为字符串并使用 charAt(x) 计算校验位实际上更容易一些。

关于java - 编译错误,整数太大。信用卡分析.Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19639640/

相关文章:

java - 我们可以在网络中运行 JavaFX 桌面应用程序吗?

python - 如何在子类中调用父类?

ruby - 为什么 `next` 没有列为 `Enumerator` 类的方法?

c# - 在 asp.net 网站中为多个 session 用户交换内容

Java 对实例和静态方法中实例和静态变量用法的说明?

Java - 重写 hashCode 和 toString

java - 重启后 BroadcastReceiver 不工作

java - 如何确保多次单击单个按钮不会重复结果?

methods - 如何向 Julia 中的现有函数添加方法?

powershell - 调用自定义类的静态方法