我已经创建了一个java扑克程序,我可以查看是否玩家或经销商是否有一对,如果他们这样做,赢家可以获得他们的账户余额。我现在想要得到它,以便如果一个玩家有一个皇家同花顺,该程序将读取它并将奖金放入赢家帐户。我想用一个循环。到目前为止这是我的代码
private int flush(List<PokerCard> hand)
{
List<PokerCard> handToSort = new ArrayList<PokerCard>();
Collections.copy(handToSort, hand);
Collections.sort(handToSort, new CardComparator());
{
List<PokerCard> deck = new ArrayList<PokerCard> ();
for (int i = 0; i < 4; i ++)
{
for (int j = 8; j < 13; j ++)
{
System.out.println("You have a Royal Flush 10 through Ace!!");
return flush;
}
else (int j = 0; j < 6; j++)
{
System.out.println("You have a straigth 2 through 6!!");
return flush;
}
else (int j = 1; j < 7; j++)
{
System.out.println("You have a straigth 3 through 7!!");
return flush;
}
else (int j = 2; j < 8; j ++)
{
System.out.println("You have a straigth 4 through 8!!");
return flush;
}
else (int j = 3; j < 9; j++)
{
System.out.println("You have a straigth 5 through 9!!");
return flush;
}
else (int j = 4; j < 10; j++)
{
System.out.println("You have a straigth 6 through 10!!");
return flush;
}
else (int j = 5; j < 11; j++)
{
System.out.println("You have a straigth 7 through Jack!!");
return flush;
}
else (int j =6; j < 12; j++)
{
System.out.println("You have a straigth 8 through Queen!!");
return flush;
}
else (int j = 7; j < 13; j ++)
{
System.out.println("You have a straigth 9 through King!!");
return flush;
}
它显示为此代码的错误,但我看不出有什么问题。我对编码很新,给自己设定了一个挑战,但是我的咬伤比我能咀嚼更多
答案 0 :(得分:0)
您的计划有一些问题。
首先,您正在错误地使用for循环和if-else语句。你可能打算写的更像是这样:
for(int i=0;i<4;i++)
{
if(handToSort.get(i).value >= 8 || handToSort.get(i).value <=13)
{
System.out.println("You have a Royal Flush 10 through Ace!!");
}
}
第二个问题是你的函数返回一个整数,但看起来你正在尝试在编写时返回一个字符串
return flush;
您也没有检查卡的套件是什么。这是检查你是否有同花顺的要求。
答案 1 :(得分:0)
for
语句没有else
个组件。所以这......
for (int j = 8; j < 13; j ++)
{
System.out.println("You have a Royal Flush 10 through Ace!!");
return flush;
}
else (int j = 0; j < 6; j++)
{
System.out.println("You have a straigth 2 through 6!!");
return flush;
}
会变成这个......
for (int j = 8; j < 13; j ++)
{
System.out.println("You have a Royal Flush 10 through Ace!!");
return flush;
}
for (int j = 0; j < 6; j++)
{
System.out.println("You have a straight 2 through 6!!");
return flush;
}
看起来您可能想要返回一个布尔值而不是整数,这会将函数的签名更改为此...
private boolean flush(List<PokerCard> hand)
for (int j = 8; j < 13; j ++)
{
System.out.println("You have a Royal Flush 10 through Ace!!");
return true;
}
for (int j = 0; j < 6; j++)
{
System.out.println("You have a straight 2 through 6!!");
return true;
}
在所有支票的最后,您需要添加return false
。这里有一个布尔值的其他替代品,但似乎最适合这个问题,“这只手是冲洗吗?”
该功能还有许多可以消除的冗余代码,并且仍然可以执行相同的工作。一旦你开始工作,你应该付出一些努力。