java中while循环中的数据问题

时间:2011-03-02 20:47:12

标签: java while-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 = currentIndex - 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");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

确定您的更新代码有点棘手(即使使用正确的格式),但如果我理解它在这里尝试做什么是一种更简单的方法来实现这一点(我在这里滥用RuntimeExceptions):

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

13345678的测试会返回有效的信用卡(支票总额为35)。至于你的代码出错的地方,看起来你的totalDigit的赋值正在将它的值重置为最后执行的循环。我当然假设当你说只添加最后一笔金额时,这就是你所指的。如果我错了,您可以更改我的代码并获得与您编码完全相同的行为(只需更少的全局变量)

答案 1 :(得分:0)

我不知道currentIndex的起始值是什么,但是如果它是0或1,则循环的第一次迭代会将其设置为负区域,并且您的while条件需要它大于或等于0。

简而言之,如果currentIndex从0或1开始,则只有在程序退出循环之前才会进行一次迭代。