gpt4 book ai didi

java - 如何将第三位玩家添加到我的简单 Java 骰子游戏中?

转载 作者:行者123 更新时间:2023-11-30 04:01:37 25 4
gpt4 key购买 nike

我希望有人能指出我正确的方向。下面的代码是我的两人骰子游戏程序。我需要添加第三个玩家,但不知道如何添加。到目前为止,我们已经介绍了 if else 语句、开关和循环 - 所以我不允许使用其他任何东西,因为我们还没有介绍它。我已经查看了这些问题,但没有找到任何可以回答我的问题的内容,有人可以帮忙吗?

import java.util.Scanner;

class dice {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String fP, sP;
int dice = 0, dfPTot = 0, dsPTot = 0, round = 0, fPScore = 0, sPScore = 0, fPScoreR = 0,
sPScoreR = 0;

System.out.println(
"Welcome to the online interactive dice game.\n\n\t
* To complete the 5 rounds you will need two players and three dice
!");
System.out.println("\nPlayer 1, please state your name: ");
fP = input.next();
System.out.println("Welcome " + fP + "\n\nPlayer 2, please state your name: ");
sP = input.next();
System.out.println("Welcome " + sP + "\n\nLet's begin!");

for (int count = 1; count <= 5; count++) {
System.out.print(fP + " please throw your three dices and then input
your total dice score
: ");
dfPTot = input.nextInt();
System.out.print(sP + " please throw your three dices and then input
your total dice score
: ");
dsPTot = input.nextInt();
round = dfPTot + dsPTot;
System.out.print(" The round total is: " + round + " \n");

if (dfPTot > dsPTot) {
fPScore = fPScore + round;
fPScoreR += fPScore;
sPScore = sPScore + 0;
sPScoreR += sPScore;
} else {
sPScore = sPScore + round;
sPScoreR += sPScore;
fPScore = fPScore + 0;
fPScoreR += fPScore;
}
}

dfPTot = fPScore = round = 0;
fPScore = fPScore + fPScoreR;
sPScore = sPScore + sPScoreR;

if (fPScore > sPScore) {
System.out.println(fP + " is the Dice Master scoring: " + fPScore + " points");
} else {
System.out.println(sP + " is the Dice Master scoring: " + sPScore + " points");
}
}
}

最佳答案

(假设您不能使用数组、对象等)

您希望永远不必像平常那样设计它。无论如何,几乎所有名称中带有 sP(第二名玩家)和 fP(第一名玩家)的变量都需要有第三个,最好是 tP(第三名玩家)。

然后您可以开始复制粘贴并更改最终 if-else 语句中的逻辑以适应第三个玩家。

(告诉你这样做真的很伤人,只是保证将来永远不会设计这样的东西):)

关于java - 如何将第三位玩家添加到我的简单 Java 骰子游戏中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com