Java一维数组

时间:2019-03-05 04:59:25

标签: java arrays

输入1到50之间的整数:1 2 1 0
1次出现2次
2次出现1次
1次出现2次

我怎样做才能让1次出现1次? 问题是它要打印很多次。

import java.util.Scanner;
public class ex3 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] num = new int[100];
        int i = 0;
        System.out.print("Enter the integers between 1 and 50: ");
        num[i] = input.nextInt();
        while(num[i] != 0){
            i++;
            num[i] = input.nextInt();
        }
        for(int j=0;j<i;j++){
            int n = 0;
            for(int k=0;k<i;k++){
                if(num[j] == num[k]){
                    n++;
                }
            }

            System.out.println(num[j] + " occurs " + n + " times");
        }
    }
}

编辑此代码

2 个答案:

答案 0 :(得分:1)

尝试一下(有关说明,请参阅代码注释):

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int[] num = new int[100];
        int i = 0;
        while (i < 100) { // Check if the array is already full
            System.out.print("Enter 0 to Exit or enter the integers between 1 and 50 (Input #" + (i + 1) + ") : ");
            int value = input.nextInt();
            if (value == 0) {
                break;
            }
            if (value < 1 || value > 50) { // check if input is between 1 and 50
                System.out.println("Input is not between 1 and 50");
            } else {
                num[i] = value;
                System.out.println();
            }

            i++;
        }
        System.out.println();
        System.out.println("Result: ");
        for (int j = 0; j < i; j++) {
            int n = 0;
            boolean isAlreadyPrinted = false; // flag to check if will be printed or not
            for (int k = 0; k < i; k++) {
                if (num[j] == num[k]) {
                    if (j > k) { // this means that the same value is already found and printed
                        isAlreadyPrinted = true;
                    }
                    n++;
                }

            }

            if (!isAlreadyPrinted) {
                System.out.println(num[j] + " occurs " + n + " times");
            }
        }
    }

}

答案 1 :(得分:0)

问题出在您的for循环上。

您不应将j的值加到i。这就是为什么“ 1发生2次”打印两次的原因。您要做的是在执行打印部件之前检查数组的特定索引的值是否已发生多次。

public static<T> T[] subArray(T[] array, int beg, int end) {
    return Arrays.copyOfRange(array, beg, end + 1);
}

public static boolean hasDuplicateValues (int[] array, int value )
{
    boolean result = false ; 
    int count = 0 ; 
    for (int i=0 ; i< array.length; i++)
    {
           if(array[i] == value)
           {
            count = count+1 ; 
           }

    }

    if(count > 1)
    {
      result = true; 
    }

   return result; 
}

public static void main(String[] args)
{
 Scanner input = new Scanner(System.in);
     int[] num = new int[100];
     int i = 0;
     System.out.print("Enter the integers between 1 and 50: ");
     num[i] = input.nextInt();
    while(num[i] != 0){
        i++;
        num[i] = input.nextInt();
    }
    for(int j=0;j<i;j++){
        int n = 0;
        for(int k=0;k<i;k++){
            if(num[j] == num[k]){
                n++;
            }
        }


        int[] subarray = subArray(num, 0, i);
        boolean isDuplicate = hasDuplicateValues (subarray , num[i] )
    if(isDuplicate == false )
    { 
          System.out.println(num[j] + " occurs " + n + " times");
    }


    }



}