Java语法问题求助:money报错、数组及do-while语句错误
Hey there, let's work through these two Java syntax issues step by step—they're super common pitfalls when you're getting the hang of the language!
That error Syntax error on token "money", @ expected after this token usually pops up when your variable declaration is in the wrong place or formatted incorrectly. Let's break down the possible causes and fixes:
- Wrong placement: If you're declaring
moneyas a member variable, make sure it's directly inside your class (not stuck in a broken method structure), and use a valid access modifier if needed (likeprivate,public, or leave it default). - Accidental method-like syntax: Sometimes beginners mix up variable declarations with method signatures—for example, writing
public int money;is fine, but adding extra parentheses or misplaced keywords will trigger errors.
Here's a correct example that avoids the error:
import java.util.*; public class Wallet { // Member variable declaration private int money; public static void main(String[] args) { // Local variable declaration inside a method int money = 500; System.out.println("You have: " + money); } }
Double-check line 23 where the error occurs—make sure money isn't trapped in a broken method header or outside the class structure entirely.
The error Insert while(Expression): to complete Do statement is straightforward: Java requires every do block to be paired with a while condition at the end. The do-while loop has strict syntax—you can't have a standalone do block.
Wrong code example (what you might have written):
do { System.out.println("Looping..."); } // Missing the while() clause here!
Correct code example:
int counter = 0; do { System.out.println("Current count: " + counter); counter++; } while (counter < 10); // Don't forget the condition and trailing semicolon!
Just add while(yourBooleanCondition); right after the closing curly brace of the do block, and make sure your condition evaluates to a boolean value.
内容的提问来源于stack exchange,提问作者Coded_overflow

