2013-10-30 62 views
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); 

回答

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

cardNum7聲明的DO-而塊。它沒有特定的代碼塊之外的存在,所以你不能在這裏訪問:

} while (cardNum7 != 0); 

你可以在做,而之前宣佈cardNum7使其while語句訪問。

相關問題