import java.util.Scanner;
public class SneakyDice
{
public static void main(String[] args)
{
game();
}
public static void game()
{
int player1;
int player2;
int p1Final=0;
int p2Final=0;
boolean gameStatus = true;
Die idie1 = new Die();
Die idie2 = new Die();
Die idie3 = new Die();
Die idie4 = new Die();
if (idie1.getNum()==idie2.getNum())
{
if(idie1.getNum()==1)
{
p1Final = 99;
}
else if(idie1.getNum()==2)
{
p1Final = 8;
}
else if(idie1.getNum()==3)
{
p1Final = 12;
}
else if(idie1.getNum()==4)
{
p1Final = 16;
}
else if(idie1.getNum()==5)
{
p1Final = 20;
}
else if(idie1.getNum()==6)
{
p1Final = 24;
}
}
else
{
p1Final = idie1.getNum() + idie2.getNum();
}
player1 = idie1.getNum() + idie2.getNum();
if (idie3.getNum()==idie4.getNum())
{
if(idie3.getNum()==1)
{
p2Final = 99;
}
else if(idie3.getNum()==2)
{
p2Final = 8;
}
else if(idie3.getNum()==3)
{
p2Final = 12;
}
else if(idie3.getNum()==4)
{
p2Final = 16;
}
else if(idie3.getNum()==5)
{
p2Final = 20;
}
else if(idie3.getNum()==6)
{
p2Final = 24;
}
}
else
{
p2Final = idie3.getNum() + idie4.getNum();
}
player2 = idie3.getNum() + idie4.getNum();
while(gameStatus == true)
{
System.out.println("Player 1's roll:" + player1);
System.out.println("Player 2's roll:" + player2);
System.out.println("Player 1's real roll:" + p1Final);
System.out.println("Player 2's real roll:" + p2Final);
System.out.println("Enter the player who rerolls: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(input == "p1" || input =="1")
{
Die die1 = new Die();
Die die2 = new Die();
if (die1.getNum()==die2.getNum())
{
if(die1.getNum()==1)
{
p1Final = 99;
}
else if(die1.getNum()==2)
{
p1Final = 8;
}
else if(die1.getNum()==3)
{
p1Final = 12;
}
else if(die1.getNum()==4)
{
p1Final = 16;
}
else if(die1.getNum()==5)
{
p1Final = 20;
}
else if(die1.getNum()==6)
{
p1Final = 24;
}
}
else
{
p1Final = die1.getNum() + die2.getNum();
}
player1 = die1.getNum() + die2.getNum();
}
else if(input =="p2" || input =="2")
{
Die die3 = new Die();
Die die4 = new Die();
if (die3.getNum()==die4.getNum())
{
if(die3.getNum()==1)
{
p2Final = 99;
}
else if(die3.getNum()==2)
{
p2Final = 8;
}
else if(die3.getNum()==3)
{
p2Final = 12;
}
else if(die3.getNum()==4)
{
p2Final = 16;
}
else if(die3.getNum()==5)
{
p2Final = 20;
}
else if(die3.getNum()==6)
{
p2Final = 24;
}
}
else
{
p2Final = die3.getNum() + die4.getNum();
}
player2 = die3.getNum() + die4.getNum();
}
else if(input == "stop" || input == "s")
{
System.out.println("This exists");
gameStatus = false;
}
}
if(p1Final>p2Final) //player1>player2
{
System.out.println("Player One Wins");
}
if(p2Final>p1Final)
{
System.out.println("Player Two Wins");
}
else
{
System.out.println("Tie");
}
}
}
此代码的初始运行工作,因为它能够打印出每个播放器的纯卷,以及它们是否适合特殊情况,它的实际值。我的代码永远不能使它成为gameStatus = false或更改播放器的滚动但是因为它似乎忽略了我在控制台中键入的内容。但是,当我删除特殊情况时,我可以按照我的预期方式工作,在这种情况下我会使骰子卷大于应有的大小。
答案 0 :(得分:0)
您的问题出在下面的代码行中,
if(input == "p1" || input =="1")
input
,"p1"
,"1"
等是字符串对象,您应该使用equals
方法来执行比较。
将其更改为,
if(input.equals("p1") || input.equals("1"))
对以下内容进行相同的更改,
else if(input =="p2" || input =="2")
else if(input == "stop" || input == "s")
将这些更改为,
else if(input.equals("p2") || input.equals("2"))
else if(input.equals("stop") || input.equals("s"))
答案 1 :(得分:0)
虽然在Java String比较期间有时会使用“==”,但最好只使用.equals()方法。
这个答案(https://stackoverflow.com/a/513839/8021378)可以解释完整的推理。
因此,您的输入都不会等于您设置的条件,并且循环将使用相同的参数再次运行并以其开头输入。这会让你觉得你的控制台输入被忽略了,只是它们只是不相等。
此外,您将要小心设置最终的if语句。截至目前,你有:
if(p1Final>p2Final) //player1>player2
{
System.out.println("Player One Wins");
}
if(p2Final>p1Final)
{
System.out.println("Player Two Wins");
}
else
{
System.out.println("Tie");
}
通过此设置,如果玩家1获胜,它将打印出“玩家一个赢”以及打印“领带”。这是因为你使用第二个if语句启动一个全新的if语句块。
要解决这个问题,只需更改:
if(p2Final>p1Final)
要:
else if(p2Final>p1Final)
这应该可以解决你的问题。