基本的 Java 数组元素比较未能正确工作。

huangapple go评论82阅读模式
英文:

Basic java array element comparision not working correctly

问题

public class Solution {

    public static void main(String[] args) {
        Solution a = new Solution();
        int nums[] = {8,1,2,2,3};
        a.smallerNumbersThanCurrent(nums);
    }

    public int[] smallerNumbersThanCurrent(int[] nums) {
        for(int i=0; i<nums.length; i++){
            int counter = 0;
            for(int j=0; j<nums.length; j++){
                int a = nums[j];
                int b = nums[i];
                if((b > a) && (a != b) && (i != j)) {
                    counter++;
                    System.out.print(b + " is bigger than " + j + " ");
                    System.out.println("Counter =" + counter + " Array[" + i + "] is bigger than array[" + j+ "]");
                }
            }
            nums[i] = counter;
        }
        return nums;
    }
}
英文:

Im creating a program which, for each array element , returns the number of elements in the array which are smaller than the array element.

The program works perfectly until it says that 2 > 2 which is false when the program is run.

I can't see where i went wrong in the code.

public class Solution {

public static void main(String[] args) {
	Solution a = new Solution();
	int nums[] = {8,1,2,2,3};
	a.smallerNumbersThanCurrent(nums);

}
 public int[] smallerNumbersThanCurrent(int[] nums) {
       for(int i=0; i&lt;nums.length ; i++){
           int counter = 0;
           for(int j =0; j&lt;nums.length; j++){
        	 int a = nums[j];
        	 int b = nums[i];
        	 if((b&gt;a) &amp;&amp; (a !=b) &amp;&amp; (i !=j)) {
        		 counter++;
        		 System.out.print(b +&quot; is bigger than &quot;+ j +&quot; &quot;);
        		 System.out.println(&quot;Counter =&quot; + counter + &quot; Array[&quot; + i + &quot;] is bigger than array[&quot; + j+ &quot;]&quot;);
        	 }
           }
           nums[i] = counter;
       }
        return nums;
    }

}

答案1

得分: 0

我编辑了你的代码以使其正确运行。在你的一些地方我有些困惑:

if((b > a) && (a != b) && (i != j))

a != bi != j 是多余的,所以我会从 if 语句中移除 i != j

然后你有这一段:

nums[i] = counter;

老实说,我真的搞不清楚为什么你的代码中有这一行,但是你最终会在第一个 for 循环中间改变你的值。这也是导致你问题的原因。
这是我稍微编辑过的代码,我相信这样可以得到正确的输出:

public class Solution {

    public static void main(String[] args) {
        Solution a = new Solution();
        int nums[] = {8,1,2,2,3};
        a.smallerNumbersThanCurrent(nums);
    }

    public int[] smallerNumbersThanCurrent(int[] nums) {
        for(int i = 0; i < nums.length ; i++) {
            int counter = 0;
            for(int j = 0; j < nums.length; j++) {
                int a = nums[j];
                int b = nums[i];
                if((b > a) && (a != b)) {
                    counter++;
                    System.out.print(b + " 比 " + j + " 大 ");
                    System.out.println("Counter =" + counter + " 数组[" + i + "] 比数组[" + j+ "]");
                }
            }
        }
        return nums;
    }
}

现在的输出是:

8 比 1 大 Counter =1 数组[0] 比数组[1]
8 比 2 大 Counter =2 数组[0] 比数组[2]
8 比 3 大 Counter =3 数组[0] 比数组[3]
8 比 4 大 Counter =4 数组[0] 比数组[4]
2 比 1 大 Counter =1 数组[2] 比数组[1]
2 比 1 大 Counter =1 数组[3] 比数组[1]
3 比 1 大 Counter =1 数组[4] 比数组[1]
3 比 2 大 Counter =2 数组[4] 比数组[2]
3 比 3 大 Counter =3 数组[4] 比数组[3]
英文:

I edited your code to make it work correctly. I was a bit confused on a couple of things you have:

`if((b&gt;a) &amp;&amp; (a !=b) &amp;&amp; (i !=j))`

a !=b and i !=j is redundant, so i'd remove the i!=j from the if statement

then you have this:

nums[i] = counter;

I honestly can't figure out why this line is in your code, but you end up changing you values in the middle of the first for loop with it. It is also what is causing your issue.
here is slightly edited code that I believe gives you the proper output

public class Solution {

public static void main(String[] args) {
    Solution a = new Solution();
    int nums[] = {8,1,2,2,3};
    a.smallerNumbersThanCurrent(nums);
	}

 public int[] smallerNumbersThanCurrent(int[] nums) {
       for(int i=0; i&lt;nums.length ; i++){
           int counter = 0;
           for(int j =0; j&lt;nums.length; j++){
             int a = nums[j];
             int b = nums[i];
             if((b&gt;a) &amp;&amp; (a != b)) {
                 counter++;
                 System.out.print(b +&quot; is bigger than &quot;+ j +&quot; &quot;);
                 System.out.println(&quot;Counter =&quot; + counter + &quot; Array[&quot; + i + &quot;] is bigger than array[&quot; + j+ &quot;]&quot;);
             }
           }
       }
        return nums;
    }
}

the output now reads:

8 is bigger than 1 Counter =1 Array[0] is bigger than array[1]
8 is bigger than 2 Counter =2 Array[0] is bigger than array[2]
8 is bigger than 3 Counter =3 Array[0] is bigger than array[3]
8 is bigger than 4 Counter =4 Array[0] is bigger than array[4]
2 is bigger than 1 Counter =1 Array[2] is bigger than array[1]
2 is bigger than 1 Counter =1 Array[3] is bigger than array[1]
3 is bigger than 1 Counter =1 Array[4] is bigger than array[1]
3 is bigger than 2 Counter =2 Array[4] is bigger than array[2]
3 is bigger than 3 Counter =3 Array[4] is bigger than array[3]

huangapple
  • 本文由 发表于 2020年10月8日 03:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64250802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定