简体   繁体   中英

program that reads integers between 1 and 100 and counts the occurrence of each

Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output should be in ascending order. Assume the input ends when the user enters a 0.

Hi guys, I know that this question has been posted before, perhaps a lot of times, but as I am a complete beginner at java, I don't completely understand the complexity of the codes that are posted. I just started taking Java classes, and would appreciate if you could help me figure out how to get my program to output the correct occurrences at the end. I'm so close to getting the answer but I can't figure it out for the life of me!! Thanks in advance!

import java.util.Scanner; 
public class Problem1 {

public static void main(String[] args) {

    //declarations 
    int [] myArray = new int [100]; 
    int input = 5; 
    Scanner keyboard = new Scanner(System.in);

    //input and processing 
    System.out.println("Please enter integers between 1 and 100 (enter 0 to stop): ");
    while (input != 0)
    {

        input = keyboard.nextInt();

        for (int i = 0; i < myArray.length; i++)
        {
            if (input == i)
            {
                myArray[i] = input; 
            }
        }

    }

    //output (This is where I need help!!!!) 
    for (int k = 0; k < myArray.length; k++)
    {
        if (myArray[k] != 0)
        {

            System.out.print(k + " occurs " + myArray[k] + " time");
            if (myArray[k] > 1)
            {
                System.out.println("s");
            }
            else 
                System.out.println("");

        }
    }
    keyboard.close(); 
}

}

You are storing the number entered by the user in the array. Instead, you should store a counter in each position of the array for the corresponding integer. When the user inputs a number, you should increase the corresponding counter.

The second part of your code (output results) seems ok. It is the first one that needs fixing.

I think the first for loop should be something like this:

for (int i = 0; i < myArray.length; i++)
    {
        if (input == i)
        {
            myArray[i] += 1; 
        }
    }

}

This should store add 1 to the array everytime the numbers occurs.

hey this my source code that worked out or me.

package test2;

import java.util.Arrays;
import java.util.Scanner; 

public class Test2 {

    public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);

    // ask for user to input numbers
    System.out.println("Enter some integers between 1 and 100 (and 0 when done): ");

    int[] myArray = new int[1000];//create a new array for user inputs

    int number;//variable for user inputs
    int count = 0;

    do
    {
        number = input.nextInt();
        myArray[count] = number;
        count++;
    }

    while (number != 0); 
    int[] mySort = new int [count - 1]; //create a new array with only the numbers 
   for(int i = 0; i< (count-1); i++) { //get the array until 0th number into new

      mySort[i] = myArray[i];
    }

    java.util.Arrays.sort(mySort);// sort the array in ascending order

    int n = 0;

    for(int i = 0; i < mySort.length; i++) {//check if the number have checked before
        int occurance = 0;
        if(n >= mySort[i]) {
            continue;
        }
        else {
        n = mySort[i];//if a new number found do the calculation+ 
        for (int j=0; j<mySort.length; j++) 
            if (n == mySort[j]) 
              occurance++; 
        System.out.print(n +  " occurs " + occurance );
        {
            if (occurance == 1) {
            System.out.println(" time.");
            }
            else {
                System.out.println(" times.");
            }
            }
        }
    }

    }

}

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