开发者

problem with data in a while loop in java

running this code, I am unable to have the number add properly. it only adds the data from the last loop

public class P4_3
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter your 8 digit Credit Card Number ");
        String cardNumber = in.next();

        if (cardNumber.length() == 8)
        {
            int totalFirst = 0;
            int currentIndex = cardNumber.length() - 1;
            int secondIndex = cardNumber.length() -2;
            int secondDouble = 0;
            String collectDigit;
            int digitSecond = 0;
            int digitFirst = 0;
            int totalDigit = 0;

            while (currentIndex >= 0)
            {       
                int smallValue = Character.digit(cardNumber.charAt(currentIndex), 10);
                totalFirst = totalFirst + smallValue;
                currentIndex = currentIn开发者_运维技巧dex - 2;
                int secondValue = Character.digit(cardNumber.charAt(secondIndex), 10);
                secondDouble = secondValue * 2;
                secondIndex = secondIndex - 2;
                System.out.println("second double " + secondDouble);
                collectDigit = Integer.toString(secondDouble);

                if (collectDigit.length() == 2)
                {
                    digitFirst = Character.digit(collectDigit.charAt(0), 10);
                    digitSecond = Character.digit(collectDigit.charAt(1), 10);
                    totalDigit = digitFirst + digitSecond;
                    System.out.println("First digit " + digitFirst);
                    System.out.println("Second digit " + digitSecond);
                }
                else 
                {
                    digitFirst = Character.digit(collectDigit.charAt(0), 10);
                    totalDigit = digitFirst;
                }        
            }

            int checkNumber = totalDigit + totalFirst;
            System.out.println("Card check number = " + checkNumber);

            if (checkNumber % 5 == 0)
            {
                System.out.println("The final digit of Check number is 0" + checkNumber);
            }
            else
            {
                System.out.println("The credit card number is not valid " + checkNumber);
            }
        }
        else
        {
            System.out.println("Credit Card number not correct length");
        }
    }
}


OK your updated code is a little tricky to follow (even with proper formatting), but if I understand what it is trying to do here is a easier way to accomplish this might be (and I'm abusing RuntimeExceptions here):

public class P4_3_simple {
  public static int checkNumber(String cardNumber) {
    int size = cardNumber.length();
    int sum_firsts = 0;
    int sum_seconds = 0;

    if(size == 8) {
      for(int index = 7; index >= 0; index -= 2) {
        int first = Character.digit(cardNumber.charAt(index), 10);
        int second = Character.digit(cardNumber.charAt(index - 1), 10) * 2;
        sum_firsts += first;
        sum_seconds += addDigits(second);
      }
    } else {
      throw new RuntimeException("Credit Card number not correct length");
    }

    return sum_firsts + sum_seconds;
  }

  public static int addDigits(int value) {
    int reduced = value;
    if(reduced > 9) {
      reduced = 0;
      char[] digits = Integer.toString(value, 10).toCharArray();
      for(char digit : digits) {
        reduced += Character.digit(digit, 10);
      }
    }

    return reduced;
  }

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter your 8 digit Credit Card Number ");
    int checkNumber = checkNumber(in.next());
    System.out.println("Card check number = " + checkNumber);
    if (checkNumber % 5 == 0) {
      System.out.println("The final digit of Check number is 0" + checkNumber);
    } else {
      System.out.println("The credit card number is not valid " + checkNumber);
    }
  }
}

A test of 13345678 returns a valid credit card (with a check sum of 35). As for where your code went wrong it looks like your assignments of totalDigit are resetting it's value to the last executed loop. I'm assuming of course that that is what you are referring to when you say only the last sum is added. If I'm wrong about that you could change my code and get the exact same behavior as you code (just with fewer global vars)


I don't know what value currentIndex is starting at, but if it's 0 or 1 then the first iteration through the loop will set it into negative territory, and your while condition demands for it to be greater than or equal to 0.

In brief, if currentIndex starts at 0 or 1, you'll only get one iteration before the program exits the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜