2012-10-05 26 views
1

因此,这是分配:使用8位数字 •从最右边的数字开始信用卡验证 - 技术上正确但错误w /算法?

信用卡号码检查,形成每一个其他数字的总和。例如,如果信用卡号码是4358 9795,那么您可以构成总和5 + 7 + 8 + 3 = 23. •将前面步骤中未包含的每个数字加倍。添加结果数字的所有数字。例如,使用上面给出的数字,将数字翻倍,从倒数第二位开始,产生18 18 10 8.将这些数值中的所有数字相加得到1 + 8 + 1 + 8 + 1 + 0 + 8 = 27. •添加前两个步骤的总和。如果结果的最后一位数字为0,则该数字有效。在我们的例子中,23 + 27 = 50,所以这个数字是有效的。 - 有效/无效?

我已经创建了一个测试程序和一个单独的方法。所有编译成功的应该不会有任何技术错误,但即使输入有效卡号时,程序也会返回该号码无效,因此我假设实际算法出现问题,但我很新这让我无法弄清楚它是什么。

import java.util.Scanner; 

public class CreditCardTester{ 

public static void main(String[] args){ 

    Scanner scanner = new Scanner(System.in); 
    String retry = ("y"); 
    String n = null; 

    while(retry.equalsIgnoreCase("y")){ // allows program to keep running even if the user enters in a capital Y 
     int lengthCheck = 1; 
     System.out.println("Please enter your 8-digit credit card number"); 

    // check to see whether the input number is exactly 8 digits 
     while(lengthCheck==1){ 
      n = scanner.next(); 
       if(n.length()==8) 
        lengthCheck=0; 
       else 
        System.out.println("Please make sure the credit card number you have entered is 8 digits"); 
     } // end inner while loop 

    // instantiate CardNumber and check validity 
    CardNumber number = new CardNumber(n); 
    number.check(); 
    if (number.isValid()) 
     System.out.println("The number you entered is a valid credit card number."); 
    else 
     System.out.println("The number you entered is not a valid credit card number. Would you like to enter in another number? (y/n)"); 
    retry = scanner.next(); 

    } // end outer while loop 

} 

} 

和单独的类

public class CardNumber { 

private String number; 
private boolean valid; 

public CardNumber(String n){ 
    number = n; 
    valid = true; 
} 

private void check1(){ 
    int a = Integer.parseInt(number.substring(7)); 
    int b = Integer.parseInt(number.substring(5,6)); 
    int c = Integer.parseInt(number.substring(3,4)); 
    int d = Integer.parseInt(number.substring(1,2)); 

    int oddsum = a + b + c + d; 

    int e = (Integer.parseInt(number.substring(6,7))) * 2; 
    int f = (Integer.parseInt(number.substring(4,5))) * 2; 
    int g = (Integer.parseInt(number.substring(2,3))) * 2; 
    int h = (Integer.parseInt(number.substring(0,1))) * 2; 

    String ee = String.valueOf(e); 
    String ff = String.valueOf(f); 
    String gg = String.valueOf(g); 
    String hh = String.valueOf(h); 

    int evensum = (Integer.parseInt(ee.substring(0))) + (Integer.parseInt(ff.substring(0))) + (Integer.parseInt(gg.substring(0))) + (Integer.parseInt(hh.substring(0))); 

    int totalsum = evensum + oddsum; 
    if (!(totalsum%10 == 0)) 
    valid=false; 
} 

public void check(){ 
    check1(); 
} 

public boolean isValid(){ 
    return valid; 
} 

} 

我敢肯定还有一个更好的方法来做到这一点,所以一切都是赞赏的建议!

+3

尝试用调试器单步,看看这是怎么回事,以及它是否是你期望的。使用问题中的示例。将数字分成数字的方法比通过字符串的往返方式更好 - 使用'/ 10'和'%10'。当你写'.substring(0)'时你打算做什么? – Rup

+0

运行你的prg,在输出中也看不到任何错误 – Satya

+1

为什么你将e转换为字符串ee,然后将其转换回整数? 'int evensum = e + f + g + h;'对我来说看起来不错。 –

回答

1

信用卡号码验证称为Luhn算法。这里有一个java implementaion http://www.xinotes.org/notes/note/595/

为您的代码,我认为在这里:

int evensum = (Integer.parseInt(ee.substring(0))) + (Integer.parseInt(ff.substring(0))) + (Integer.parseInt(gg.substring(0))) + (Integer.parseInt(hh.substring(0))); 

你的意思是要总结两digist:

int evensum = Integer.parseInt(ee.substring(0,1)) + Integer.parseInt(ee.substring(1)) ... 
+1

你有额外的不必要的括号。 –

+0

@SteveKuo我的回答更多的是暗示他的算法可能是错的。我在回答开始时提供了更好的实施。谢谢 –

0

试试这个。

public class CardNumber { 

    String number; 
    boolean valid; 

    public CardNumber(String n){ 
     number = n; 
    } 

    public void check(){ 
     // The odd sum 
     // For the eight digits, 1, 3, 5, 7 are odd 
     int a = Integer.parseInt("" + number.charAt(1)); 
     int b = Integer.parseInt("" + number.charAt(3)); 
     int c = Integer.parseInt("" + number.charAt(5)); 
     int d = Integer.parseInt("" + number.charAt(7)); 

     int oddsum = a+b+c+d; 

     // The even sum 
     int e = (Integer.parseInt(number.substring(6,7))) * 2; 
     int f = (Integer.parseInt(number.substring(4,5))) * 2; 
     int g = (Integer.parseInt(number.substring(2,3))) * 2; 
     int h = (Integer.parseInt(number.substring(0,1))) * 2; 

     // As suggested by RUP to make it more simple 
     int evensum = e + f + g + h; 

     // Total sum 
     int totalsum = oddsum + evensum; 
     valid = (totalsum%10==0)?true:false;    
    } 

    public boolean isValid(){ 
     return valid; 
    } 

} 
+0

为什么要将e,f,g,h转换为字符串,然后再转回来? – Rup

+0

谢谢。我复制了他的源代码并进行了修改。 –