简体   繁体   English

如何确定哈希表中的一对值是否满足条件?

[英]How do I determine if a pair of values in a hashtable answers a condition?

I am trying to enter pairs of values (i,j) in an array to determine how many of those pairs respect the following condition: 1 <= i < j <= length of the array.我试图在数组中输入值对 (i,j) 以确定其中有多少对符合以下条件:1 <= i < j <= 数组的长度。 The output is rarely the right answer, I do not know why.输出很少是正确答案,我不知道为什么。 Can anyone help me?谁能帮我? Here is my code:这是我的代码:

public static void main(String[] args) {
        int oldVal[][] = new int [20][2];
        boolean loopAgain = true;
        Scanner scan = new Scanner(System.in);
        int num = 0;
        int i = 0;
        do {
            System.out.print("Enter input 1:");
            int input1 = scan.nextInt();

            System.out.print("Enter input2:");
            int input2 = scan.nextInt();
                
                oldVal[i][0] = input1;
                oldVal[i][1] = input2;
                i++;

            System.out.print("Enter another input (y/n)?");
            String answer = scan.next();

            if (answer.equals("y") || answer.equals("Y")) {
                continue;
            } else {
                for (int x = 0; x < oldVal.length; x++) {
                    if (oldVal[x][0] <= 1 && oldVal[x][0] < oldVal[x][1] && oldVal[x][1] <= 20) {
                        num++;
                    }
                }
                System.out.print("Output number is %d.\n" + num);
                break;
            }
            
        } while (loopAgain);
        scan.close();
    }
}

Thank you!谢谢!

Your if condition is bad.你的 if 条件不好。 Your pseudo code states that 1 <= i, however, your if condition states i <= 1, so changing the if would solve the issue:您的伪代码声明 1 <= i,但是,您的 if 条件声明 i <= 1,因此更改 if 可以解决问题:

if (1 <= oldVal[x][0]  && oldVal[x][0] < oldVal[x][1] && oldVal[x][1] <= 20) {
   num++;
}

On the other hand, your if condition to check for a "y" input has a bad design, you should simply negate the condition and get rid of the continue statement.另一方面,检查“y”输入的 if 条件设计不好,您应该简单地否定条件并摆脱 continue 语句。 I also would use a regex expression so the user may type y, Y, yes, Yes, yeah, Yeah, YEAH, YES, and all of them would work.我还会使用正则表达式,这样用户可以输入 y、Y、是、是、是、是、是、是、是,所有这些都可以工作。 Also, the for loop may be improved with a for each (or enhanced for).此外,可以使用 for each(或增强 for)来改进 for 循环。 Also, for the sake of code readability you should declare the num variable inside the if block and before the for loop.此外,为了代码可读性,您应该在 if 块内和 for 循环之前声明 num 变量。 ALSO, if you use the break statement to leave the while loop after printing your output whats the use of loopAgain?另外,如果在打印输出后使用 break 语句离开 while 循环,loopAgain 的用途是什么? Just write while(true) , or better add a loopAgain=false instead of break.只需编写while(true) ,或者最好添加一个loopAgain=false而不是 break。 Adding all those changes would result in:添加所有这些更改将导致:

if (!answer.matches("[yY].*")) {
   int num = 0;
   for (int[] ints : oldVal) {
      if (1 <= ints[0] && ints[0] < ints[1] && ints[1] <= 20) {
         num++;
      }
   }
   System.out.printf("Output number is %d.%n", num);
   loopAgain=false;
}

Also, note that if you want to use the %d stuff, you should invoke System.out.printf (not println), and instead of \\n you should use %n.另外,请注意,如果你想使用 %d 的东西,你应该调用 System.out.printf(不是 println),而不是 \\n 你应该使用 %n。

Now that it works fine, I suggest you to update to java 16 and research about new ways of doing it (use a Stream<int[]> instead of a bi-dimensional array, or use a HashMap<Integer, Integer>, use lambdas, Stream.filter, Stream.count, and use var instead of explicit types in local variables).现在它工作正常,我建议你更新到 java 16 并研究做它的新方法(使用 Stream<int[]> 而不是二维数组,或使用 HashMap<Integer, Integer>,使用lambdas、Stream.filter、Stream.count,并在局部变量中使用 var 而不是显式类型)。 It'll help you.它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM