Java扑克游戏中的直手
我很难完成扑克牌的直法。我不明白为什么我的代码不起作用Java扑克游戏中的直手,java,Java,我很难完成扑克牌的直法。我不明白为什么我的代码不起作用 public static boolean containsStraight(int [] hand) { boolean straight = false; for(int i = 0; i < 5; i++) { if (hand[i] == 2 && hand[i] == 3 && hand[i] == 4 && hand[i] == 5 &&
public static boolean containsStraight(int [] hand)
{
boolean straight = false;
for(int i = 0; i < 5; i++)
{
if (hand[i] == 2 && hand[i] == 3 && hand[i] == 4 && hand[i] == 5 && hand[i] == 6)
{
straight = true;
}
if (hand[i] == 3 && hand[i] == 4 && hand[i] == 5 && hand[i] == 6 && hand[i] == 7)
{
straight = true;
}
if (hand[i] == 4 && hand[i] == 5 && hand[i] == 6 && hand[i] == 7 && hand[i] == 8)
{
straight = true;
}
if (hand[i] == 5 && hand[i] == 6 && hand[i] == 7 && hand[i] == 8 && hand[i] == 9)
{
straight = true;
}
}
return straight;
public静态布尔containssStraight(int[]hand)
{
布尔直线=假;
对于(int i=0;i<5;i++)
{
if(hand[i]==2&&hand[i]==3&&hand[i]==4&&hand[i]==5&&hand[i]==6)
{
直=真;
}
if(hand[i]==3&&hand[i]==4&&hand[i]==5&&hand[i]==6&&hand[i]==7)
{
直=真;
}
if(hand[i]==4&&hand[i]==5&&hand[i]==6&&hand[i]==7&&hand[i]==8)
{
直=真;
}
if(hand[i]==5&&hand[i]==6&&hand[i]==7&&hand[i]==8&&hand[i]==9)
{
直=真;
}
}
直线返回;
}你说在每次迭代中[i]必须是2和3,4和5。这是不可能的。手上只有一个数字[i]。你说在每次迭代中,手[i]必须是2和3,4和5。这是不可能的。手头只有一个数字[i]。正如pL4Gu33在他的回答中所说的,您的比较是错误的。本质上,for循环中的每一步都会将
hand[i]
保留为一个常量值(例如,4)。这意味着您的if语句正在检查:
if(4 == 4 && 4 == 5 && 4 == 6 && 4 == 7 && 4 == 8) {
...
}
这永远不会被认为是真的。如果你确实知道你手上有五个元素,而且手已经被分类了,你可以这样做
不过,我会给你一个更好的答案
你应该做的第一件事是整理你的手。一旦你这样做了,你就可以很容易地跨过手牌,检查手牌中的下一张牌是否恰好比上一张牌大一张。如果你到了终点,这是真的,那么这是一条直线
/*
* You will need to import java.util.Arrays
*/
public boolean isStraight(int[] hand) {
if(hand == null || hand.length != 5) {
return false;
}
else {
// Automatically sort the hand
Arrays.sort(hand);
// Set the "previous" variable to a theoretically impossible value
int prev = -1;
// Iterate through the hand and see if the next card is exactly one more than
// the previous one.
for(int i = 0; i < hand.length; i++) {
// If prev is -1, then this is the first time through the for-loop
// If the card that we're on has a value of the previous card + 1,
// we still have the possibility of a straight.
if(prev == -1 || (prev + 1) == hand[i]) {
prev = hand[i];
}
else {
return false;
}
}
return true;
}
}
/*
*您需要导入java.util.array
*/
公共布尔值isStraight(int[]手){
if(hand==null | | hand.length!=5){
返回false;
}
否则{
//自动分类
数组。排序(手动);
//将“previous”变量设置为理论上不可能的值
int prev=-1;
//反复检查手牌,看看下一张牌是否正好比下一张牌多一张
//前一个。
对于(int i=0;i
正如pL4Gu33在其回答中所述,您的比较是错误的。本质上,for循环中的每一步都会将hand[i]
保留为一个常量值(例如,4)。这意味着您的if语句正在检查:
if(4 == 4 && 4 == 5 && 4 == 6 && 4 == 7 && 4 == 8) {
...
}
这永远不会被认为是真的。如果你确实知道你手上有五个元素,而且手已经被分类了,你可以这样做
不过,我会给你一个更好的答案
你应该做的第一件事是整理你的手。一旦你这样做了,你就可以很容易地跨过手牌,检查手牌中的下一张牌是否恰好比上一张牌大一张。如果你到了终点,这是真的,那么这是一条直线
/*
* You will need to import java.util.Arrays
*/
public boolean isStraight(int[] hand) {
if(hand == null || hand.length != 5) {
return false;
}
else {
// Automatically sort the hand
Arrays.sort(hand);
// Set the "previous" variable to a theoretically impossible value
int prev = -1;
// Iterate through the hand and see if the next card is exactly one more than
// the previous one.
for(int i = 0; i < hand.length; i++) {
// If prev is -1, then this is the first time through the for-loop
// If the card that we're on has a value of the previous card + 1,
// we still have the possibility of a straight.
if(prev == -1 || (prev + 1) == hand[i]) {
prev = hand[i];
}
else {
return false;
}
}
return true;
}
}
/*
*您需要导入java.util.array
*/
公共布尔值isStraight(int[]手){
if(hand==null | | hand.length!=5){
返回false;
}
否则{
//自动分类
数组。排序(手动);
//将“previous”变量设置为理论上不可能的值
int prev=-1;
//反复检查手牌,看看下一张牌是否正好比下一张牌多一张
//前一个。
对于(int i=0;i
问题在于,您使用的周期不正确,因为您总是在手[i]中检查同一张卡的值。我的建议是要么先进行排序,要么如果你想提高效率,你可以使用第二个布尔字段,这将表明你手中是否有给定值的卡片。这样,你可以很容易地检查,如果你有任何数量的卡在连续
public static boolean containsStraight(int[] cards) {
int count = 0;
boolean[] valueInHand = new boolean[10];
for (int card : cards) {
valueInHand[card] = true;
}
for (boolean value : valueInHand) {
if (value == true) {
count++;
} else {
count = 0;
}
// works for any number of cards
if (count == cards.length) {
return true;
}
}
return false;
}
问题是,您使用的周期不正确,因为您总是在手[i]中检查同一张卡的值。我的建议是要么先进行排序,要么如果你想提高效率,你可以使用第二个布尔字段,这将表明你手中是否有给定值的卡片。这样,你可以很容易地检查,如果你有任何数量的卡在连续
public static boolean containsStraight(int[] cards) {
int count = 0;
boolean[] valueInHand = new boolean[10];
for (int card : cards) {
valueInHand[card] = true;
}
for (boolean value : valueInHand) {
if (value == true) {
count++;
} else {
count = 0;
}
// works for any number of cards
if (count == cards.length) {
return true;
}
}
return false;
}
请定义并扩展“不工作”。请定义并扩展“不工作”。您可以将其用于(int i=0;i