首页 > 解决方案 > 未调用主方法的方法(Java)

问题描述

我正在为班级分配制作火柴游戏,并且需要使用除主要方法之外的不同方法。

玩家对战电脑,可以选择1-4场比赛,电脑必须永远赢。

在我的 IDE 上一切似乎都很好,除了我无法将 PLAYER() 和 COM() 方法调用回我的 main 方法。它打印以下内容:

错误:(25, 13) java: 类 matchsticks 中的方法 PLAYER 不能应用于给定类型;必需:int、java.lang.String、int 找到:无参数原因:实际参数列表和形式参数列表的长度不同

请帮忙,谢谢阅读。这是我的代码:

public class matchsticks {
    Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String player;
        int match;
        int total = 0;

        System.out.println("");
        System.out.println("Welcome! RULES: ");
        System.out.println("The computer (COM) and player take turns picking up 1-4 matchsticks. There are 21 total.");
        System.out.println("The one who picks up the LAST matchstick loses. Ready to play?");

            System.out.println();
            System.out.println("Name?: ");
            player = keyboard.next();



            // PLAYER method call to the main method. Not working.
            // Tried putting in parameters in () and didn't work.
            System.out.println("");
            System.out.print(player + "'s turn: ");
            match = keyboard.nextInt();
            PLAYER();


            //COMplay method call to main method. Not working.
            //Also tried putting parameters in () and it didn't help.
            System.out.println("");
            System.out.print("COM's turn: ");
            COMplay();

            //Loss output
            printLOSE(total);

    }

    //PLAYER//
    public static void PLAYER(int total, String player, int match) {
        Scanner keyboard = new Scanner(System.in);
        while (total < 21) {
            while (match > 4) {
                System.out.print("Nice try, but against the rules. Try again.");
                System.out.println(player+"'s turn: ");
                match = keyboard.nextInt();
            }
            total += match;

            System.out.println("");
            System.out.println(player+ "'s pick: " +match);
            System.out.println("Matches taken:  " +total);
            return;
        }
    }

    public static void COMplay(int match, int total) {
        if (match == 1) {
            total += 4;
            System.out.println("");
            System.out.println("COM's pick: 4");
            System.out.println("Matches taken:  " +total);
        }
        if (match == 2) {
            total += 3;
            System.out.println("");
            System.out.println("COM's pick: 3");
            System.out.println("Matches taken:  " +total);
        }
        if (match == 3) {
            total += 2;
            System.out.println("");
            System.out.println("COM's pick: 2");
            System.out.println("Matches taken:  " +total);
        }
        if (match == 4) {
            total += 1;
            System.out.println("");
            System.out.println("COM's pick: 1");
            System.out.println("Matches taken:  " +total);
        }
        return;
    }

    public static void printLOSE(int total) {
        if (total >= 21) {
            System.out.println("LOSE.");
            return;
        }
    }
}

标签: java

解决方案


PLAYER(total, player, match);

COMplay(match, total);

你说这个吗?对我有用,但你需要一个do-while循环来完成你的游戏。Bc 你只有一次机会挑选火柴,而且还不够

编辑

    public static void COMplay(int match, int total) {
    switch(match) {
        case 1: 
            total += 4;
            System.out.println("\nCOM's pick: 4");
            break;

        case 2:
            total += 3;
            System.out.println("\\nCOM's pick: 3");
            break;

        case 3:
            total += 2;
            System.out.println("\nCOM's pick: 2");
            break;

        case 4:
            total += 1;
            System.out.println("\nCOM's pick: 1");
        break;

        default:// nothing to do here.
    }

    System.out.println("Matches taken:  " +total);
    // You dont need a return statement
}

通过这种方式很容易阅读


推荐阅读