该代码工作正常。但是,有一个小问题,如果后两个数字相同,则代码表示没有数字匹配。例如:
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 == b
,b == c
和a == 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/