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

运行MethodHolder类报错缺少static void main方法,该如何添加?

解决方法

在Java中,static void main(String[] args)是程序的入口方法,你可以把它放在**MethodHolder类的内部,和其他静态方法同级的位置**——不管是类开头、中间还是结尾,只要它是类的直接成员(不嵌套在其他方法内)就行。

以下是添加了main方法的完整代码(同时修复了原代码中Scanner输入的换行符问题):

import java.util.Scanner;

public class MethodHolder {

    private static int nextShopperID = 100;
    private static int nextProductID = 100;

    protected static Shopper generateShopper() {
        Scanner scan = new Scanner(System.in);

        System.out.print("Name: ");
        String name = scan.nextLine();

        System.out.print("Balance: ");
        double balance = scan.nextDouble();

        System.out.print("Product Limit: ");
        int productLimit = scan.nextInt();
        // 处理nextInt后遗留的换行符,避免影响后续输入
        scan.nextLine();

        return new Shopper(nextShopperID++, name, balance, productLimit);
    }

    protected static Product generateProduct() {
        Scanner scan = new Scanner(System.in);

        System.out.print("Name: ");
        String name = scan.nextLine();

        System.out.print("Department: ");
        String department = scan.nextLine();

        System.out.print("Description: ");
        String description = scan.nextLine();

        System.out.print("Price: ");
        double price = scan.nextDouble();

        System.out.print("Initial Count: ");
        int initialCount = scan.nextInt();
        // 处理nextInt后遗留的换行符
        scan.nextLine();

        return new Product(nextProductID++, name, department, description, price, initialCount);
    }

    protected static void printShopperInfo(Shopper s) {
        System.out.println("Name: " + s.getName());
        System.out.println("Products Bought: " + s.getProductsBought());
        System.out.println("Can Still Buy: " + (s.getProductLimit() - s.getProductsBought()));
        System.out.println("Spent So Far: $" + s.getSpentSoFar());
        System.out.println("Balance Remaining: $" + s.getBalance());
        System.out.println("Products Purchased: ");
        s.printProducts();
    }

    protected static void printProductInfo(Product p) {
        System.out.println("Name: " + p.getName());
        System.out.println("Department: " + p.getDepartment());
        System.out.println("Description: " + p.getProductDescription());
        System.out.println("Price: $" + p.getPrice());
        System.out.println("Number Bought: " + p.getNumberBought());
        System.out.println("Number Remaining: " + (p.getInitialCount() - p.getNumberBought()));
        System.out.println("Product Bought By: ");
        p.printShoppers();
    }

    protected static void doPurchase(Shopper s, Product p) {
        if (s.canAdd() && p.canAdd()) {
            if (s.addProduct(p) && p.addShopper(s))
                System.out.println(s.getName() + " has purchased " + p.getName());
            else
                System.out.println(s.getName() + "'s purchase of " + p.getName() +
                        " could not be completed");
        } else {
            if (!s.canAdd())
                System.out.println(s.getName() + " cannot purchase any more items");
            if (!p.canAdd())
                System.out.println("There are no more of " + p.getName() + " in the store");
        }
    }

    // 新增的程序入口main方法
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        // 示例流程:创建购物者、商品,执行购买并打印信息
        System.out.println("=== 创建购物者 ===");
        Shopper shopper = generateShopper();
        
        System.out.println("\n=== 创建商品 ===");
        Product product = generateProduct();
        
        System.out.println("\n=== 执行购买 ===");
        doPurchase(shopper, product);
        
        System.out.println("\n=== 购物者信息 ===");
        printShopperInfo(shopper);
        
        System.out.println("\n=== 商品信息 ===");
        printProductInfo(product);
        
        scan.close();
    }
}

内容的提问来源于stack exchange,提问作者Ethan Huang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 20:13:16