提问人:Alice333 提问时间:2/17/2023 更新时间:2/17/2023 访问量:9
使用来自另一个类的参数访问方法(不继承)
Accessing a method with parameters from another class (without inheritance)
问:
我正在做一个小型的 DiceGame 项目,目前我正在努力访问方法,主要来自游戏类,其中包含游戏的“规则”以及玩家和骰子的数量。 例如,对于结束游戏的方法,我想访问一个返回掷骰子完整结果的方法。但是在这里,我收到一个错误,因为这些方法包含用于访问有关 DiceCup 中包含的投掷结果信息的参数。 有什么方法可以修复参数或我的访问方法的方式?
public void endGame() {
printWinner();
getSumWithDoublets(); -- ERROR HERE
Scoreboard scoreboard = Scoreboard.getInstance();
scoreboard.updateScoreboard(player);
System.out.print("Game over");
}
public int getSumWithDoublets(int numOfDoublets, int doublets) {
getSum(); -- HERE I get the same error, also because of parameters in the method
instanceDiceCup.getDoublets();
System.out.println("This round you made a total of" + sum);
//doublets
if (numOfDoublets >= 2) {
//adding sum and doublets
System.out.println("Highest doublet is a " + doublets + "It contains" + numOfDoublets + " dices.");
System.out.println("You are getting " + sum + " multiplied with " + doublets + "and" + numOfDoublets);
sumWithDoublets = sum * doublets * numOfDoublets;
}
//whole result
return sumWithDoublets;
}
答: 暂无答案
上一个:传递来自用户的参数 [已关闭]
下一个:如何访问带有参数的方法?
评论