如何移除Java程序骨架中的静态方法及替换方案?
Fixing Static Methods for Your Dog Daycare Program
Hey there! I see you're stuck converting those static methods to instance members for your school assignment—let's break this down step by step. The core idea is to shift all your static logic into instance-level code, since the assignment only allows main to be static.
Key Changes Needed
- Convert the static
Scannerinto an instance variable - Turn all static helper methods (
initialize,runCommandLoop,readCommand,handleCommand,closeDown) into non-static instance methods - Create an instance of
ProgramSkeletoninmainand use that to call all your methods - Fix the
exitcommand logic to properly terminate the loop withoutSystem.exit
Modified Code
import java.util.Scanner; public class ProgramSkeleton { // Instance-level Scanner instead of static private Scanner input; // Constructor to initialize the Scanner public ProgramSkeleton() { this.input = new Scanner(System.in); } public void initialize() { System.out.println("Welcome to the dog register!"); System.out.println("Write 0 to register new dog"); System.out.println("Write 1 to increase age"); System.out.println("Write 2 to list dogs"); System.out.println("Write 3 to remove dog"); System.out.println("Write 4 or 'exit' to exit"); } public void runCommandLoop() { boolean done; do { String command = readCommand(); done = handleCommand(command); } while (!done); } public String readCommand() { System.out.print("> "); return input.nextLine().trim(); // Trim whitespace for better command handling } private boolean handleCommand(String command) { switch (command.toLowerCase()) { // Make command case-insensitive (nice touch!) case "0": case "register new dog": System.out.println("You have chosen register new dog."); return false; // Don't exit the loop after this command case "1": case "increase age": System.out.println("You have chosen increase age."); return false; case "2": case "list dogs": System.out.println("You have chosen list dogs."); return false; case "3": case "remove dog": System.out.println("You have chosen remove dog."); return false; case "4": case "exit": return true; // Signal the loop to terminate default: System.out.println("Unknown command. Try again!"); return false; } } public void closeDown() { System.out.println("Goodbye!"); input.close(); // Clean up the Scanner resource } public static void main(String[] args) { // Create an instance of our class to use instance methods ProgramSkeleton dogDaycare = new ProgramSkeleton(); dogDaycare.initialize(); dogDaycare.runCommandLoop(); dogDaycare.closeDown(); // The program will exit naturally here without System.exit } }
What Changed & Why
- Instance Scanner: We moved
Scannerinto the class as a private instance variable, initialized in the constructor. This avoids static state, which aligns with your assignment rules. - Non-static Methods: All helper methods are now instance methods—they operate on the instance of
ProgramSkeleton, which holds the Scanner we need. - Command Logic Fix: I adjusted the
handleCommandmethod to returnfalsefor all non-exit commands (so the loop keeps running) andtrueonly for exit commands. This properly terminates the loop without needingSystem.exit. - Cleanup: Added
input.close()incloseDown()to properly release the Scanner resource—good practice! - Case Insensitivity: Made the command check case-insensitive with
toLowerCase()so users can type "Exit" or "EXIT" and it still works (optional but user-friendly).
This code now meets all your assignment requirements: no static methods/variables outside of main, supports short/long commands, and the exit command correctly terminates the program with a goodbye message.
内容的提问来源于stack exchange,提问作者crispycat
相关产品推荐
相关产品推荐

