为什么我一直在为丢失的符号收到编译错误?

时间:2013-10-30 19:50:33

标签: java

错误:

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

2 个答案:

答案 0 :(得分:4)

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

} while (cardNum7 != 0);

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

答案 1 :(得分: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);