Java 布尔方法不断返回true

Java 布尔方法不断返回true,java,boolean,Java,Boolean,请帮助我理解为什么我编写的布尔方法没有正确返回。我要求用户输入一系列整数,这些整数将存储在一个数组中,该数组将检查是否存在重复项 import java.util.Scanner; public class PokerHands { public static void main(String args[]) { System.out.println(containsPair(welcome())); } // end main privat

请帮助我理解为什么我编写的布尔方法没有正确返回。我要求用户输入一系列整数,这些整数将存储在一个数组中,该数组将检查是否存在重复项

import java.util.Scanner;

public class PokerHands 
{
    public static void main(String args[])
    {
        System.out.println(containsPair(welcome()));
    } // end main

    private static int[] welcome()
    {
        Scanner read = new Scanner(System.in);
        int[] poker_array = new int[5];

        System.out.println("Enter five numeric cards, no face cards. Use 2-9: ");
        for (int i = 0; i < 5; i++) {
            System.out.printf("Card %d: ", i + 1);
            int nums = read.nextInt();
            while (nums < 2 || nums > 9) {
            System.out.println("Wrong input. Choose from 2-9 only: ");
            nums = read.nextInt();
            } // end while
        poker_array[i] = nums;
        }

        for (int i = 0; i < poker_array.length; i++) {
            System.out.print(poker_array[i] + ", ");
        }

        return poker_array;
    } // end welcome()

    private static boolean containsPair(int hand[])
    {
        for (int i = 0; i < hand.length; i++) {
            for (int j = 0; j < hand.length; j++) {
                if (hand[i] == hand[j]) {
                    return true;
                } // end if
            } // end inner for
        } // end outer for

        return false;
    } // end containsPair()
} //end class
import java.util.Scanner;
公营扑克手
{
公共静态void main(字符串参数[])
{
System.out.println(containsPair(welcome());
}//末端总管
私有静态int[]欢迎()
{
扫描仪读取=新扫描仪(System.in);
int[]扑克数组=新int[5];
System.out.println(“输入五张数字卡,不输入正面卡。使用2-9:”);
对于(int i=0;i<5;i++){
系统输出打印(“卡%d:,i+1”);
int nums=read.nextInt();
而(nums<2 | | nums>9){
System.out.println(“输入错误。仅从2-9中选择:”);
nums=read.nextInt();
}//结束时
扑克数组[i]=nums;
}
for(int i=0;i
输出: $java扑克手

输入五张数字卡,无正面卡。使用2-9:

卡1:2

卡2:3

卡3:4

卡4:5

卡5:6

2,3,4,5,6,正确

$java扑克手 输入五张数字卡,无正面卡。使用2-9:

卡1:2

卡2:2

卡3:3

卡4:4

卡5:5

2,2,3,4,5,对

$java扑克手

输入五张数字卡,无正面卡。使用2-9:

卡1:2

卡2:2

卡3:2

卡4:2

卡5:2


2,2,2,2,2,2,真的

这是因为第一次通过时,
i=0
j=0
。因此,它将始终返回真实。您应该初始化
j=i+1

这是因为对于第一次通过,
i=0
j=0
。因此,它将始终返回真实。您应该初始化
j=i+1

用HashSet用法更改方法。 代码将是这样的

private static boolean containsPair(int hand[])
{
    //adding to list all elements
    List<Integer> intList = new ArrayList<>();
    for (int i : hand) {
        intList.add(i);
    }
    //put all elements to set
    HashSet<Integer> zipcodeSet = new HashSet<>(intList);
    //if size not match , has a dublicate
    return zipcodeSet.size() != hand.length;

} // end containsPair()
private静态布尔containsPair(int-hand[])
{
//添加到列表中的所有元素
List intList=new ArrayList();
用于(int i:手动){
添加(i);
}
//设置所有元素
HashSet-zipcodeSet=新的HashSet(intList);
//如果尺寸不匹配,则具有双精度
返回zipcodeSet.size()!=hand.length;
}//end containsPair()

使用HashSet用法更改方法。 代码将是这样的

private static boolean containsPair(int hand[])
{
    //adding to list all elements
    List<Integer> intList = new ArrayList<>();
    for (int i : hand) {
        intList.add(i);
    }
    //put all elements to set
    HashSet<Integer> zipcodeSet = new HashSet<>(intList);
    //if size not match , has a dublicate
    return zipcodeSet.size() != hand.length;

} // end containsPair()
private静态布尔containsPair(int-hand[])
{
//添加到列表中的所有元素
List intList=new ArrayList();
用于(int i:手动){
添加(i);
}
//设置所有元素
HashSet-zipcodeSet=新的HashSet(intList);
//如果尺寸不匹配,则具有双精度
返回zipcodeSet.size()!=hand.length;
}//end containsPair()

尝试将布尔值中的if更改为以下值:

if (hand[i] == hand[j] && i != j)

尝试将布尔值中的if更改为以下值:

if (hand[i] == hand[j] && i != j)

i
j
将从
0
开始,因此您将比较相同的元素,该元素将等于其本身,并且该方法将返回true
i
j
将从
0
开始,因此您将比较相同的元素,它将等于自身,并且该方法将返回trueinitialize j=i+1,但是array[i]=array[j]与array[j]=array[i]相同吗?你是什么意思?请通过更改j=i+1来检查您的程序。我做了。我只是想了解j=I+1代表什么。它是i[0]上的int旁边的int吗?如果i=0,j将是1。因此,您将开始比较数组[0]和数组[1]等等..正如@Amit所说的。当i=0时,手牌[i]是第一张牌。j=i+1,所以j是1。手牌[j]是第二张牌。当j增加时,你将第一张牌与第二张牌进行比较,然后第一张牌与第三张牌进行比较,然后第一张牌与第四张牌进行比较,直到你到达手牌的末端。初始化j=i+1,但是数组[i]=数组[j]与数组[j]=数组[i]相同吗?你的意思是什么?请通过更改j=i+1来检查您的程序。我做了。我只是想了解j=I+1代表什么。它是i[0]上的int旁边的int吗?如果i=0,j将是1。因此,您将开始比较数组[0]和数组[1]等等..正如@Amit所说的。当i=0时,手牌[i]是第一张牌。j=i+1,所以j是1。手牌[j]是第二张牌。当j增加时,你将第一张牌与第二张牌进行比较,然后第一张牌与第三张牌进行比较,然后第一张牌与第四张牌进行比较,直到你到手为止。你还应该解释为什么它会改变结果。你还应该解释为什么它会改变结果。