gpt4 book ai didi

java - 老虎机 - 输出不正确

转载 作者:行者123 更新时间:2023-12-01 18:22:54 26 4
gpt4 key购买 nike

代码运行良好。然而有一个小问题,如果后两个数字相同,则代码表示没有数字匹配。例如:

0 2 2没有匹配的数字再玩一次? (是/否?)

显然应该说:

0 2 2两个数字匹配再玩一次? (是/否?)

如何更改代码以实现此目的?

谢谢

import java.util.Random;
import java.util.Scanner;

public class SlotMachine {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int slotOne;
int slotTwo;
int slotThree;
String userResponse;

do{

Random randomNumbers = new Random();

slotOne = randomNumbers.nextInt(10);
slotTwo= randomNumbers.nextInt(10);
slotThree = randomNumbers.nextInt(10);


System.out.println(slotOne + " " + slotTwo + " " + slotThree + " " );
if (slotOne != slotTwo && slotOne != slotThree)
{
System.out.println("No numbers match");
}
else if (slotOne == slotTwo && slotOne == slotThree)
{
System.out.println("All three match - jackpot");
}
else
{
System.out.println("Two numbers match");
}

System.out.print("Play again? (Y/N?)");
userResponse = scan.next();
}

while (userResponse.equalsIgnoreCase("Y"));

do{
if(userResponse.equalsIgnoreCase("N"))

{
System.out.print("Thank You.");
userResponse = scan.next();
}
else if (!userResponse.equalsIgnoreCase("Y"))
{
System.out.print("Play again? (Y/N?)");
userResponse = scan.next();
}
}
while (!userResponse.equalsIgnoreCase("Y"));

scan.close();

}
}

最佳答案

我建议你计算匹配项,a == bb == ca == c,例如

System.out.println(slotOne + " " + slotTwo +  " " + slotThree + " " );  
int count = 0;
if (slotOne == slotTwo) count++;
if (slotOne == slotThree) count++;
if (slotTwo == slotThree) count++;
if (count == 3) {
System.out.println("All three match - jackpot");
} else if (count > 0) {
System.out.println("Two numbers match");
} else {
System.out.println("No numbers match");
}

关于java - 老虎机 - 输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27178676/

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