(Java)需要帮助将整数集中的数字相加并将其应用于信用卡验证算法

时间:2016-03-20 04:31:42

标签: java loops while-loop

我必须创建一个java程序,根据它是Visa还是MasterCard来验证信用卡号。我需要将卡号读作单个字符串,每组四位数之间有空格(xxxx xxxx xxxx xxxx)。必须有16位数字。必须汇总该号码中的所有数字。然后,如果总和%10 = 0,则它是有效的签证。如果总和%10 = 1,则它是有效的万事达卡。任何偏离都会导致无效消息。

我的问题是,一旦我运行当前程序,我输入数字和卡类型,然后程序停止并且不会继续。我不确定我在这里做错了什么。

import java.util.Scanner;
public class Assignment4
{
    public static void main (String[] args)
    {
        String cardNum;
        String typeAnswer;
        char cardType;
        int testSum;
        int modResult;

        Scanner scan = new Scanner (System.in);

        System.out.println("\t\t Credit Card Verification");
        System.out.println("\t\t ========================");
        System.out.println("Enter your card number <xxxx xxxx xxxx xxxx>: ");
        cardNum = scan.nextLine();

        if(cardNum.length()<19 || cardNum.length()>19)
        {
            System.out.println("Incorrect card number. Re-launch the program  and enter a 16-digit card number");
            System.exit(0);
         }
        else
        {
        System.out.println("Is your card Visa or MasterCard?");
        typeAnswer = scan.next().toUpperCase();
        cardType = answer.charAt(0);

        String numSet1 = cardNum.substring(0,4);
        String numSet2 = cardNum.substring(5,9);
        String numSet3 = cardNum.substring(10,14);
        String numSet4 = cardNum.substring(15,19);


        int i = Integer.parseInt(numSet1);
        int j = Integer.parseInt(numSet2);
        int k = Integer.parseInt(numSet3);
        int l = Integer.parseInt(numSet4);

        int sum1=0;
        while(i>0)
            {
                sum1 = sum1 + (i%10);
                i = i/10;
            }
        int sum2 = 0;
        while(j>0)
            {
                sum2 = sum2 + (j%10);
                j = j/10;
            }
        int sum3 = 0;
        while(k>0)
            {
                sum3 = sum3+ (k%10);
                k = k/10;
            } 
        int sum4 = 0;
        while(l>0)
            {
               sum4 = sum4 + (l%10);
               j = j/10;
            }


        testSum = sum1 + sum2 + sum3 + sum4;
        modResult = testSum%10

        if(modResult=0 && cardType=V)
        {
            System.out.println("Valid Visa card.");

        }
        else if (modResult=1 && cardType=M)
            {
                System.out.println("Valid MasterCard.");
            }
            else
            {
                System.out.println("Not a valid " + typeAnswer + " card. Re-launch and try again.");
            }


        }


    }
}

2 个答案:

答案 0 :(得分:1)

  • answer未定义。它应该是typeAnswer
  • modResult = testSum%10之后缺少分号。
  • if语句中的条件错误:
    • 使用==运算符,而不是=运算符来比较基元类型的值。
    • 使用字符文字'V''M'代替未定义的符号VM
  • l未在第4个循环中更新,因此如果第4个数字为正,则它将是无限循环。 j = j/10;应为l = l/10;

试试这个:

import java.util.Scanner;
public class Assignment4
{
    public static void main (String[] args)
    {
        String cardNum;
        String typeAnswer;
        char cardType;
        int testSum;
        int modResult;

        Scanner scan = new Scanner (System.in);

        System.out.println("\t\t Credit Card Verification");
        System.out.println("\t\t ========================");
        System.out.println("Enter your card number <xxxx xxxx xxxx xxxx>: ");
        cardNum = scan.nextLine();

        if(cardNum.length()<19 || cardNum.length()>19)
        {
            System.out.println("Incorrect card number. Re-launch the program  and enter a 16-digit card number");
            System.exit(0);
        }
        else
        {
            System.out.println("Is your card Visa or MasterCard?");
            typeAnswer = scan.next().toUpperCase();
            cardType = typeAnswer.charAt(0);

            String numSet1 = cardNum.substring(0,4);
            String numSet2 = cardNum.substring(5,9);
            String numSet3 = cardNum.substring(10,14);
            String numSet4 = cardNum.substring(15,19);


            int i = Integer.parseInt(numSet1);
            int j = Integer.parseInt(numSet2);
            int k = Integer.parseInt(numSet3);
            int l = Integer.parseInt(numSet4);

            int sum1=0;
            while(i>0)
            {
                sum1 = sum1 + (i%10);
                i = i/10;
            }
            int sum2 = 0;
            while(j>0)
            {
                sum2 = sum2 + (j%10);
                j = j/10;
            }
            int sum3 = 0;
            while(k>0)
            {
                sum3 = sum3+ (k%10);
                k = k/10;
            } 
            int sum4 = 0;
            while(l>0)
            {
               sum4 = sum4 + (l%10);
               l = l/10;
            }


            testSum = sum1 + sum2 + sum3 + sum4;
            modResult = testSum%10;

            if(modResult==0 && cardType=='V')
            {
                System.out.println("Valid Visa card.");
            }
            else if (modResult==1 && cardType=='M')
            {
                System.out.println("Valid MasterCard.");
            }
            else
            {
                System.out.println("Not a valid " + typeAnswer + " card. Re-launch and try again.");
            }

        }

    }
}

请注意,此程序中有更多问题需要修复。例如,此程序接受-123 -456 -789 -147作为有效的Visa卡。

答案 1 :(得分:0)

在行中:cardType=answer.charAt(0);您有错误。变量答案未定义。它应该是cardType=typeAnswer.charAt(0); 接下来你创建了一个无限循环,即。第四个循环 while(l>0) { sum4 = sum4 + (l%10); j = j/10; } 另一个错误是你没有在if语句中使用相等,而是将值赋给变量。 正确的代码如下:

import java.util.*;
import java.io.*;
public class CreditCardVerification
{
    public static void main (String[] args)
    {
        String cardNum;
        String typeAnswer;
        char cardType;
        int testSum=0;
        int r;
        int modResult;
        Scanner scan = new Scanner (System.in);
        System.out.println("\t\t Credit Card Verification");
        System.out.println("\t\t ========================");
        System.out.println("Enter your card number <xxxx xxxx xxxx xxxx>: ");
        cardNum = scan.nextLine();
        if(cardNum.length()<19 || cardNum.length()>19)
        {
            System.out.println("Incorrect card number. Re-launch the program  and enter a 16-digit card number");
            System.exit(0);
         }
        else
        {
            System.out.println("Is your card Visa or MasterCard?");
            typeAnswer = scan.next().toUpperCase();
            cardType = typeAnswer.charAt(0);                
            String numSet1 = cardNum.substring(0,4);
            String numSet2 = cardNum.substring(5,9);
            String numSet3 = cardNum.substring(10,14);
            String numSet4 = cardNum.substring(15,19);
            int i = Integer.parseInt(numSet1);
            int j = Integer.parseInt(numSet2);
            int k = Integer.parseInt(numSet3);
            int l = Integer.parseInt(numSet4);
            int sum1=0;
            while(i!=0)
            {
                sum1 = sum1 + (i%10);
                i = i/10;
            }
            int sum2 = 0;
            while(j!=0)
            {
                sum2 = sum2 + (j%10);
                j = j/10;
            }
            int sum3 = 0;
            while(k!=0)
            {
                sum3 = sum3+ (k%10);
                k = k/10;
            } 
            int sum4 = 0;
            while(l!=0)
            {
               sum4 = sum4 + (l%10);
               l = l/10;
            }
            testSum = sum1 + sum2 + sum3 + sum4;
            modResult = testSum%10;                    
            if((modResult==0) && (cardType=='V'))
            {
                System.out.println("Valid Visa card.");              
            }
            else if ((modResult==1) && (cardType=='M'))
            {
                System.out.println("Valid MasterCard.");
            }
            else
            {
                System.out.println("Not a valid " + typeAnswer + " card. Re-launch and try again.");
                System.exit(0);
            }
        }
    }
}`

试试这个代码。它与MikeCat给出的代码没那么不同,但这可以解决你的问题。请注意,不要输入任何数字并测试代码。请使用有效的Visa或MasterCard信用卡进行试用。