0

错误:

CreditCard.java:180: error: cannot find symbol
        } while (cardNum7 != 0);
                 ^
  symbol:   variable cardNum7
  location: class CreditCard
1 error
Process javac exited with code 1

代码:

do {
    //Declarations
    long cardNum7 = 0;
    //testing
    System.out.print("Enter a card number (type 0 to end program): ");
    cardNum7 = keyboard.nextLong();
    System.out.println("Your credit card is " + isValid(cardNum7));
} while (cardNum7 != 0);
4

2 回答 2

4

cardNum7在 do-while 块内声明。它不存在于该特定代码块之外,因此您无法在此处访问它:

} while (cardNum7 != 0);

您可以在 do-while 之前声明 cardNum7 以使其可供 while 语句访问。

于 2013-10-30T19:52:41.387 回答
3

您已cardNum7在循环内声明,因此它不会反映在循环外。利用:

//Declarations
long cardNum7 = 0;
do {
    //testing
    System.out.print("Enter a card number (type 0 to end program): ");
    cardNum7 = keyboard.nextLong();
    System.out.println("Your credit card is " + isValid(cardNum7));
} while (cardNum7 != 0);
于 2013-10-30T19:52:30.827 回答