You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除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 Scanner into an instance variable
  • Turn all static helper methods (initialize, runCommandLoop, readCommand, handleCommand, closeDown) into non-static instance methods
  • Create an instance of ProgramSkeleton in main and use that to call all your methods
  • Fix the exit command logic to properly terminate the loop without System.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

  1. Instance Scanner: We moved Scanner into the class as a private instance variable, initialized in the constructor. This avoids static state, which aligns with your assignment rules.
  2. Non-static Methods: All helper methods are now instance methods—they operate on the instance of ProgramSkeleton, which holds the Scanner we need.
  3. Command Logic Fix: I adjusted the handleCommand method to return false for all non-exit commands (so the loop keeps running) and true only for exit commands. This properly terminates the loop without needing System.exit.
  4. Cleanup: Added input.close() in closeDown() to properly release the Scanner resource—good practice!
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:34:49