常见问题

时间:2017-04-06 22:19:41

标签: java arrays

我的整个代码工作正常,除了常见的部分。如果输入1个数字最多,它将起作用,但如果输入2个数字相同的次数,它将不再工作。任何有关改进我的代码的其他建议都将非常感激。

public static void Thingy() {
    Scanner input = new Scanner(System.in);
    int min = 999;
    int max = 0;
    int range;
    int prevocc = 0;
    int occ = 0;
    int common = 0;
    int enter = 1;
    int a1_5 = 0;
    int a6_10 = 0;
    int a11_15 = 0;
    int a16_20 = 0;
    int a21_25 = 0;
    int a26_30 = 0;
    int a31_35 = 0;
    int a36_40 = 0;
    int a41_45 = 0;
    int a46_50 = 0;
    int[] num;
    num = new int[99999];
    for (int i = 1; i < 99999 && enter != 0; i++) {
        System.out.println("Enter a number between 1 and 50, enter 0 to quit");
        num[i] = input.nextInt();
        enter = num[i];
        if (num[i] > max) {
            max = num[i];
        }
        if (num[i] < min) {
            if (num[i] != 0) {
                min = num[i];
            }
        }
        if (num[i] > 50) {
            System.out.println("Your number is too high, enter another number");
            num[i] = input.nextInt();
        }
        if (num[i] < 0) {
            System.out.println("Your number is too low, enter another number");
            num[i] = input.nextInt();
        }
        for (; num[i] > 0; num[i]--) {
            if (enter == num[i]) {
                occ++;
            }
            if (occ > prevocc) {
                common = num[i];
            }
            prevocc = occ;
        }

        num[i] = enter;
    }
    for (int j = 50; j > 0; j--) {
        if (num[j] >= 1 && num[j] <= 5) {
            a1_5++;
        } else if (num[j] >= 6 && num[j] <= 10) {
            a6_10++;
        } else if (num[j] >= 11 && num[j] <= 15) {
            a11_15++;
        } else if (num[j] >= 16 && num[j] <= 20) {
            a16_20++;
        } else if (num[j] >= 21 && num[j] <= 25) {
            a21_25++;
        } else if (num[j] >= 26 && num[j] <= 30) {
            a26_30++;
        } else if (num[j] >= 31 && num[j] <= 35) {
            a31_35++;
        } else if (num[j] >= 36 && num[j] <= 40) {
            a36_40++;
        } else if (num[j] >= 41 && num[j] <= 45) {
            a41_45++;
        } else if (num[j] >= 46 && num[j] <= 50) {
            a46_50++;
        }
    }
    System.out.print("1 - 5: ");
    while (a1_5 > 0) {
        System.out.print("*");
        a1_5--;
    }
    System.out.println("");
    System.out.print("6 - 10: ");
    while (a6_10 > 0) {
        System.out.print("*");
        a6_10--;
    }
    System.out.println("");
    System.out.print("11 - 15: ");
    while (a11_15 > 0) {
        System.out.print("*");
        a11_15--;
    }
    System.out.println("");
    System.out.print("16 - 20: ");
    while (a16_20 > 0) {
        System.out.print("*");
        a16_20--;
    }
    System.out.println("");
    System.out.print("21 - 25: ");
    while (a21_25 > 0) {
        System.out.print("*");
        a21_25--;
    }
    System.out.println("");
    System.out.print("26 - 30: ");
    while (a26_30 > 0) {
        System.out.print("*");
        a26_30--;
    }
    System.out.println("");
    System.out.print("31 - 35: ");
    while (a31_35 > 0) {
        System.out.print("*");
        a31_35--;
    }
    System.out.println("");
    System.out.print("36 - 40: ");
    while (a36_40 > 0) {
        System.out.print("*");
        a36_40--;
    }
    System.out.println("");
    System.out.print("41 - 45: ");
    while (a41_45 > 0) {
        System.out.print("*");
        a41_45--;
    }
    System.out.println("");
    System.out.print("46 - 50: ");
    while (a46_50 > 0) {
        System.out.print("*");
        a46_50--;
    }
    System.out.println("");
    System.out.println("The highest number is: " + max);
    System.out.println("The lowest number is " + min);
    System.out.println("The most commonly occuring number is: " + common);
    System.out.println("The range is: " + (max - min));
}

0 个答案:

没有答案