gpt4 book ai didi

java - 显示最后在 if 语句中创建的结果 - Java

转载 作者:行者123 更新时间:2023-11-30 02:05:12 25 4
gpt4 key购买 nike

我是java新手,我正在编写一个程序,它将随机化1-6 4次,显示4个骰子中的每一个,然后添加最高的3个。

我想在最后一起显示分数(统计数据),但我已经在 if 和 else if 语句中生成了统计数据。有关如何改进代码以允许我稍后使用 int 变量 stat1 stat2 等的任何建议

//stat1     
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
System.out.println("Dice 3: " + dice3);
System.out.println("Dice 4: " + dice4);

if(dice1 <= dice2 && dice1 <= dice3 && dice1 <= dice4) {
System.out.println("----------------------------------------");
int stat1 = dice2 + dice3 + dice4;
System.out.println("Stat 1: " + stat1);
}

else if(dice2 <= dice1 && dice2 <= dice3 && dice2 <= dice4) {
System.out.println("----------------------------------------");
int stat1 = dice1 + dice3 + dice4;
System.out.println("Stat 1: " + stat1);
}

else if(dice3 <= dice1 && dice3 <= dice2 && dice3 <= dice4) {
System.out.println("----------------------------------------");
int stat1 = dice1 + dice2 + dice4;
System.out.println("Stat 1: " + stat1);
}

else {
System.out.println("----------------------------------------");
int stat1 = dice1 + dice2+ dice3;
System.out.println("Stat 1: " + stat1);
}

六个统计数据中每一个的代码基本相同,但接下来的 4 个骰子变量替换了“dice1”“dice2”“dice3”“dice4”。

提前致谢

最佳答案

您可以将逻辑移至函数

 private int displayAndReturnStat(int dice1, int dice2, int dice3, int dice4, int statNum) {
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
System.out.println("Dice 3: " + dice3);
System.out.println("Dice 4: " + dice4);

int stat;
if(dice1 <= dice2 && dice1 <= dice3 && dice1 <= dice4) {
System.out.println("----------------------------------------");
stat = dice2 + dice3 + dice4;
System.out.println("Stat " + statNum + " : " + stat);
}

else if(dice2 <= dice1 && dice2 <= dice3 && dice2 <= dice4) {
System.out.println("----------------------------------------");
stat = dice1 + dice3 + dice4;
System.out.println("Stat " + statNum + " : " + stat);
}

else if(dice3 <= dice1 && dice3 <= dice2 && dice3 <= dice4) {
System.out.println("----------------------------------------");
stat = dice1 + dice2 + dice4;
System.out.println("Stat " + statNum + " : " + stat);
}

else {
System.out.println("----------------------------------------");
stat = dice1 + dice2+ dice3;
System.out.println("Stat " + statNum + ": " + stat);
}
return stat;
}

并调用为

int stat1 = displayStat(/*dice values*/, 1); //Stat1
int stat2 = displayStat(/*dice values*/, 2); //Stat2
int stat3 = displayStat(/*dice values*/, 3); //Stat3
int stat4 = displayStat(/*dice values*/, 4); //Stat4

为了使用 Streams 的乐趣(逻辑是找到最小的骰子并将除最小的骰子之外的所有骰子的值相加)

private int displayAndReturnStat(int dice1, int dice2, int dice3, int dice4, int statNum) {
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
System.out.println("Dice 3: " + dice3);
System.out.println("Dice 4: " + dice4);

int minDice = Stream.of(dice1, dice2, dice3, dice4)
.min(Comparator.naturalOrder())
.get(); //Calling get won't throw an exception here as the Stream is guaranteed to be not empty
int stat = Stream.of(dice1, dice2, dice3, dice4)
.filter(diceValue -> diceValue != minDice)
.mapToInt(diceValue -> diceValue)
.sum();
System.out.println("Stat " + statNum + ": " + stat);
return stat;
}

关于java - 显示最后在 if 语句中创建的结果 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51568651/

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