如何解决Java中的Illegal start of Expression等编译错误?
Hey there! Let's work through those three compilation errors you're facing—illegal start of expression, else without if, and illegal start of statement—using your code snippet as a guide. First, let's unpack the issues in the code you shared (I filled in the truncated part with logical, common next steps):
import java.util.Scanner; public class Temperature_Converter { public static void main(String[] args) { System.out.println("Welcome!This program takes temperature as an input and an input" + " indicating wether we're converting from Celsius to Fahrenheit or Fahrenheit to Celsius"); Scanner scan = new Scanner (System.in); System.out.println("Please enter \"C\" if we're converting from Fahrenheit to Celsius, or \"F\" for Celsius to Fahrenheit:"); char choice = scan.next().charAt(0); System.out.println("Enter the temperature value:"); double temp = scan.nextDouble(); // Here's where the errors likely occurred if (choice == 'C' || choice == 'c') double convertedTemp = (temp - 32) * 5/9; // Illegal start of expression System.out.println("Converted temperature: " + convertedTemp + " Celsius"); else // Else without if double convertedTemp = (temp * 9/5) + 32; // Another illegal start of expression System.out.println("Converted temperature: " + convertedTemp + " Fahrenheit"); } }
1. Fixing illegal start of expression
This error happens when you try to declare a variable directly after an if/else without wrapping the code block in curly braces {}. Java only treats the single line immediately after if as part of the conditional—declaring a variable there breaks the syntax rules for statement structure.
Fix: Wrap all code inside each if/else branch in curly braces. This groups statements together and lets you safely declare variables within the block.
2. Fixing else without if
This occurs when the compiler can't find a matching if for your else. In your original code, since you omitted curly braces for the if block, Java only recognizes the first line (the variable declaration) as part of the if—the System.out.println becomes a standalone statement, so the else has no associated if.
Fix: Use curly braces for both if and else blocks to clearly define the scope of each conditional branch.
3. Fixing illegal start of statement
This can come from two issues in your code:
- Unclosed string literals: Your truncated
System.out.println("Please enter \"C\" if we\'re converting from Fa...was missing a closing quote, making the compiler misinterpret all subsequent code as part of the string. - Misplaced statements: Without curly braces, lines like the
System.out.printlnend up in invalid positions outside the conditional scope.
Fix: Ensure all string literals are closed with quotes, and use curly braces to contain code within their proper blocks.
Final Fixed Code
import java.util.Scanner; public class Temperature_Converter { public static void main(String[] args) { System.out.println("Welcome! This program takes temperature as an input and an input" + " indicating whether we're converting from Celsius to Fahrenheit or Fahrenheit to Celsius"); Scanner scan = new Scanner(System.in); System.out.println("Please enter \"C\" if we're converting from Fahrenheit to Celsius, or \"F\" for Celsius to Fahrenheit:"); char choice = scan.next().charAt(0); System.out.println("Enter the temperature value:"); double temp = scan.nextDouble(); if (choice == 'C' || choice == 'c') { double convertedTemp = (temp - 32) * 5.0/9.0; // Use 5.0/9.0 to avoid integer division System.out.println("Converted temperature: " + convertedTemp + " Celsius"); } else if (choice == 'F' || choice == 'f') { double convertedTemp = (temp * 9.0/5.0) + 32; System.out.println("Converted temperature: " + convertedTemp + " Fahrenheit"); } else { System.out.println("Invalid input! Please enter 'C' or 'F'."); } scan.close(); // Clean up the scanner resource } }
Extra Tips
- I added a check for invalid inputs to make the program more user-friendly.
- Using
5.0and9.0instead of integers ensures we get a decimal result (integer division would truncate the value otherwise). - Fixed the typo "wether" to "whether" for clarity.
内容的提问来源于stack exchange,提问作者MCgenijus MCgenijus




