简体   繁体   中英

Why do I keep getting a compile error for a missing symbol?

Error:

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

Code:

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

cardNum7 is declared inside the do-while block. It doesn't exist outside that particular code block, so you can't access it here:

} while (cardNum7 != 0);

You could declare cardNum7 before the do-while to make it accessible to the while statement.

You have cardNum7 declared inside the loop so it won't be reflected outside the loop. Use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM