简体   繁体   中英

Why is my loop jumping out before it counts the last number?

The task of the program is to take integers from 1 to 100 and count how many times they occur.

For example the numbers entered by the user are 1 2 2 3 3 25 25 67 98 99 99. Output would be:

1 occurs 1 time
2 occurs 2 times
3 occurs 2 times
25 occurs 2 times
67 occurs 1 time
98 occurs 1 time

The problem

While my program will successfully count most of the numbers, it fails to count the two 99's and if I were to replace the the second to last 99 with a 98 then the output would be 98 occurs 2 times but it would not count the 99.

I have tried debugging this thing and I shouldn't say it doesn't count the last number or numbers in the array, because it does, but it jumps right out before it moves to the else-if statements.

It seems to jump out because the first if statement is true and therefore doesn't need to go to the else and it is finished with the loop but figuring out how to make it still print the last number is confusing me.

Here is the code. The issue is in the LinearSearch() method:

import java.util.Scanner;

public class Lab8 {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int usernumbers[] = new int[20];
    int size = usernumbers.length;

    System.out.println("Enter integers between 1 and 100, " + "enter a 0 to end ");

    // user populates the array usernumbers
    int i = 0;
    do {
      usernumbers[i] = input.nextInt();
    } while (usernumbers[i++] != 0);

    // invokes the method to sort the array
    arraySort(usernumbers);

    System.out.println(java.util.Arrays.toString(usernumbers));

    // this invokes the method to search the array for the numbers
    linearSearch(usernumbers);

    input.close();
  }

  public static void arraySort(int[] usernumbers) {
    // bubblesort for usernumbers[]
    int a = usernumbers.length;
    for (int i = 0; i < a - 1; i++) {
      for (int j = 0; j < a - i - 1; j++) {

        if (usernumbers[j] > usernumbers[j + 1]) {
          int temp = usernumbers[j];
          usernumbers[j] = usernumbers[j + 1];
          usernumbers[j + 1] = temp;
        }
      }
    }
  }

  public static void linearSearch(int[] usernumbers) {
    int compare = usernumbers[0];
    int count = 0;

    for (int i = 0; i < usernumbers.length; i++) {
      if (compare == usernumbers[i]) {
        count++;
      } else {
        if (count > 1 && compare != 0) {
          System.out.println(compare + " occurs " + count + " times");
        }

        if (count == 1 && compare != 0) {
          System.out.println(compare + " occurs " + count + " time");
        }

        count = 1;
        compare = usernumbers[i];
      }
    }
  }
}

The problem is that you miss the iteration of the last number.

public class Job8 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int usernumbers[] = new int[20];
        int size = usernumbers.length;

        System.out.println("Enter integers between 1 and 100, " + "enter a 0 to end ");

        // user populates the array usernumbers
        int i = 0;
        do {
            usernumbers[i] = input.nextInt();
        } while (usernumbers[i++] != 0);

        // invokes the method to sort the array
        arraySort(usernumbers);

        System.out.println(java.util.Arrays.toString(usernumbers));

        // this invokes the method to search the array for the numbers
        linearSearch(usernumbers);

        input.close();
    }

    public static void arraySort(int[] usernumbers) {
        // bubblesort for usernumbers[]
        int a = usernumbers.length;
        for (int i = 0; i < a - 1; i++) {
            for (int j = 0; j < a - i - 1; j++) {

                if (usernumbers[j] > usernumbers[j + 1]) {
                    int temp = usernumbers[j];
                    usernumbers[j] = usernumbers[j + 1];
                    usernumbers[j + 1] = temp;
                }
            }
        }
    }

    public static void linearSearch(int[] usernumbers) {
        int count = 0;
        int currentNumber = 0;
        for (int i = 0; i < usernumbers.length; i++) {
            int nextIndex = i + 1;
            currentNumber = usernumbers[i];

            if (currentNumber == 0) {
                continue;
            }

            if (nextIndex < usernumbers.length) {
                int nextNumber = usernumbers[nextIndex];
                if (currentNumber == nextNumber) {
                    count++;
                    continue;
                } else {
                    if (count == 0) {
                        System.out.println(currentNumber + " occurs once");
                    } else {
                        System.out.println(currentNumber + " occurs " + count + " times");
                        count = 0;
                    }
                }
            }
        }

        System.out.println(+currentNumber + " occurs " + (count + 1) + " times");
    }
}

Welcome to SO.

I'm afraid you've completely misread the problem in your code. The problem is that when you sort your array it moves all the zero values to the front. When you come to print the occurences in linearSearch you are assuming there are trailing zeros to force the the printing of the final value. This should be a relatively easy thing to fix but ask me a question in the comments if you want a hint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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