Eclipse无法运行代码求助:编程新手遇导入及运行错误
Hey there! Let's break down why your test code isn't running in Eclipse— I can spot a couple of clear issues from what you shared:
1. Class Name Conflict
Your class is named Calendar, but Java already has a built-in java.util.Calendar class (which you're even using indirectly via GregorianCalendar). This naming clash confuses the compiler, since it won't know which Calendar you're referring to.
Fix: Rename your class to something unique, like HireYearChecker or EmployeeCalendar.
2. Truncated/Syntactically Invalid Code
Your code cuts off at Syst...— this is almost certainly a typo for System (like trying to write System.out.println() but not finishing the statement). Incomplete syntax will immediately cause a compile error, so Eclipse can't run the code at all.
Corrected Example Code
Here's a fixed version of your code that should work smoothly:
import java.util.GregorianCalendar; import java.util.Scanner; // Renamed class to avoid conflict with Java's built-in Calendar public class HireYearChecker { public static void main(String []args) { GregorianCalendar cal1 = new GregorianCalendar(); Scanner input = new Scanner(System.in); System.out.println("Please enter your hire year (yyyy): "); int hireYear = input.nextInt(); int currYear = cal1.get(GregorianCalendar.YEAR); // Added a sample output to complete the logic System.out.println("You've been employed for " + (currYear - hireYear) + " years!"); input.close(); // Clean up the scanner resource } }
Quick Eclipse Pro Tip
Always keep an eye out for red underlines in your code— hovering over them will show you the exact error message (like "cannot resolve symbol 'Syst'" or naming conflicts). That's Eclipse's way of pointing you straight to the root of the problem!
内容的提问来源于stack exchange,提问作者Jonny Murillo

