【发布时间】:2020-06-20 08:28:36
【问题描述】:
我正在为班级分配制作一个火柴游戏,需要使用除 main 方法之外的不同方法。
玩家与电脑对战,可以选择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;
}
}
}
【问题讨论】:
-
当您尝试此操作时可能会出现特定错误...关于未提供您的方法所期望的参数的问题...
-
@David:哎呀,谢谢你让我知道。它已更新错误!
-
伙计,我把自己当小丑了。固定的参数,它的工作,谢谢人。 :)
标签: java