0

我正在尝试计算数组中存在唯一数字的次数,使用的索引数量取决于输入的元素数量。除了 1 之外,它主要是可操作的。第一个值没有被考虑在内。循环正在检查arrays.length -1,所以即使我输入了 3 两次,数字 3 也显示为 1 的计数。我知道解决这个问题的最好方法是运行一个不使用的循环,arrays.length -1但是我无法将一个条目与它旁边的条目进行比较if(a[i] == a[i + 1] && a[i] != 0)看看是否有不止一次出现的值。我认为最好的办法是将计数值与其对应的被计数的数组值一起存储在我的计数方法中,然后在该方法之外执行一个 for 循环,这可能吗?我看不到这样做的方法,因为我对 java 还很陌生。可以给我一些指导吗:)

import java.util.Scanner;


public class Prac_ExeOne 
{
        static int count  = 1;
        static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
        static int[] Array =  new int [50]; // the maximum elements inside the array that can be used is 10;
        int size;

              public int fillArray(int[] a)
         {
                 System.out.println("Enter up to " + a.length + " nonnegative numbers.");
                 System.out.println("Mark the end of the list with a negative number.");
                 Scanner keyboard = new Scanner(System.in);

                 int next;
                 int index = 0;
                 next = keyboard.nextInt();
                 while ((next >= 0) && (index < a.length ))  
                 {
                     numberUsed++;
                     a[index] = next;
                     index++;
                     // Print out each value of next
                     System.out.println(next);
                     next = keyboard.nextInt();
                     //System.out.println("Number of indexes used" + numberUsed);
                 }
                 keyboard.close(); // close the keyboard so it can't continue to be used.
                 return index;
        }


            public int[] sort(int[] arrays)
            {

                for(int i = 0;i < arrays.length -1 ;i++ )
                {

                    int store = 0;
                    // Move Larger Values to the right.
                    if (arrays[i + 1 ] < arrays[i])
                    {
                        store = arrays[i];
                        arrays[i] = arrays[i + 1];
                        arrays[i + 1] = store;
                    }
                    // Sort swapped smaller values to the left.
                        for(int j = i; j > 1; j--)
                        {
                           if (arrays[j] < arrays[j - 1])
                           {
                           store = arrays[j];
                           arrays[j] = arrays[j - 1];
                           arrays[j - 1] = store;
                           }
                      }
                }
                return arrays;

            }
            public void count(int[] a)
            {   

                //for each element in array go through if conditons.
                System.out.println("N " + "Count");
                for(int i = 0;i < a.length -1;i++)
                {   
                    if(a[i] == a[i + 1] && a[i] != 0)
                    {
                        count++;

                    }
                    if(a[i] != a[i+1])
                    {
                        count = 1;
                    }
                    if (a[i] !=  0)
                     {
                    System.out.println(a[i] + " " + count);
                     }
                }
            }

            public static void main(String[] args) 
            {
                Prac_ExeOne score = new Prac_ExeOne();
                score.fillArray(Array);
                score.sort(Array);
                score.count(Array);


            }
        }

输入:

Enter up to 50 nonnegative numbers.
Mark the end of the list with a negative number.
3
3
2
2
-2

输出:

N Count
3 1
2 2
2 1

期望的结果:

简而言之,我希望程序正确计算值,然后在 N 下方的左侧显示该值,并将其在数组中的次数显示在 Count 下方

4

3 回答 3

1

In a nutshell I want the program to count the values correctly

Count in a Map:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {

    public static void main(String[] args) throws Exception {
        List<Integer> ints = new ArrayList<>();
        ints.add(3);
        ints.add(3);
        ints.add(2);
        ints.add(2);
        ints.add(-2);
        ints.add(5);

        Map<Integer, Integer> counts = new HashMap<>();

        for (Integer i : ints) {
            if (i < 0) {
                break;
            }
            if (counts.containsKey(i)) {
                counts.put(i, counts.get(i) + 1);
            } else {
                counts.put(i, 1);
            }
        }

        System.out.println(counts);
    }
}

Output:

{2=2, 3=2}
于 2013-09-04T14:22:22.080 回答
1

对于sort函数

for (int j = i; j > 1; j--)

应该

for (int j = i; j > 0; j--)

System.out.println(Arrays.toString(Array));在调用 sort 并3在开头看到 a 之后,这使我意识到您正在跳过第一个元素。

请注意,还有更有效的排序算法

对于该count功能,您count在错误的时间重置并且过于频繁地打印。我将其更改如下:

public void count(int[] a)
{
    //for each element in array go through if conditions.
    System.out.println("N " + "Count");
    for (int i = 0; i < a.length - 1; i++)
    {   
        if (a[i] != 0)
        {
            if (a[i] == a[i + 1])
            {
                count++;
            }
            // if the next element is different, we already counted all of the
            //   current element, so print it, then reset the count
            else
            {
                System.out.println(a[i] + " " + count);
                count = 1;
            }
        }
    }
    // we haven't processed the last element yet, so do that
    if (a[a.length-1] != 0)
        System.out.println(a[a.length-1] + " " + count);
}
于 2013-09-04T14:40:50.993 回答
1

如果您真的想使用带有唯一计数器的数组,可以使用下面的代码:

public class Prac_ExeOne {
    static int count = 1;
    static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
    static Integer[] Array = new Integer[50]; // the maximum elements inside the array that can be used is 10;
    int size;

    public int fillArray(Integer[] a) {
        System.out.println("Enter up to " + a.length + " nonnegative numbers.");
        System.out.println("Mark the end of the list with a negative number.");
        Scanner keyboard = new Scanner(System.in);

        int next;
        int index = 0;
        next = keyboard.nextInt();
        while ((next >= 0) && (index < a.length)) {
            numberUsed++;
            a[index] = next;
            index++;
            // Print out each value of next
            System.out.println(next);
            next = keyboard.nextInt();
            // System.out.println("Number of indexes used" + numberUsed);
        }
        keyboard.close(); // close the keyboard so it can't continue to be used.
        return index;
    }

    public Integer[] sort(final Integer[] arrays) {
        Arrays.sort(arrays, new Comparator<Integer>() {

            @Override
            public int compare(Integer int1, Integer int2) {
                if (null != int1 && null != int2) {
                    if (int1 < int2) {
                        return -1;
                    } else if (int1 > int2) {
                        return 1;
                    } else  {
                        return 0;
                    }
                }
                return 0;
            }

        }); 
        return arrays;

    }

    public void count(Integer[] a) {

        // for each element in array go through if conditons.
        System.out.println("N " + "Count");
        for (int i = 0; i < a.length - 1; i++) {
            if (i == 0 && a[i] != null) {
                System.out.println(a[i] + " " + count);
            }
            if (i > 0 && (a[i] != null && a[i - 1] != null)) {
                if (a[i] == a[i - 1]) {
                    count++;    
                }
                if (a[i] != a[i - 1]) {
                    count = 1;
                }
                if (a[i] != 0) {
                    System.out.println(a[i] + " " + count);
                }
            } else {
                count = 1;
            }
        }
    }

    public static void main(String[] args) {
        Prac_ExeOne score = new Prac_ExeOne();
        score.fillArray(Array);
        score.sort(Array);
        score.count(Array);        
    }
}

结果:

最多输入 50 个非负数。

用负数标记列表的结尾。

3

3

2

2

-1

N 计数

2 1

2 2

3 1

3 2

由于您的排序方法,您的代码不起作用。(对不起,我的英语不好)

于 2013-09-04T14:50:50.230 回答