Lab 1

Page 376 programming project 6.1

 

CountIntegers1

 

Create an application that reads a number of integers in the range of 0 to 50 inclusive. It counts how many times each one is entered. After all input is processed print the values with the number of times each one was entered.

Use the name occurrences for the array. Use the variable ÒenteredÓ for the value the user types in.

 

More info:

  1. create an array that can hold at least 50 numbers
  2. tell the user to end input by typing a number outside the range Ð say 55.
  3. read the keyboard. For example:   int entered = Keyboard.readInt ();
  4. continue to get values from the user ( while loop ) while entered >= 0 and <= 50 also put the values into the array.
  5. report all integers that were entered one or more times. Code follows.

 

System.out.println ("Number\tTimes");

      for (int check = 0; check <= 50; check++)

         if (occurrences [check] >= 1)

            System.out.println (check + "\t" + occurrences [check]);

 

OUTPUT

 

 

 

After you get the program to run, explain how the code fragment I supplied works.

( counts how many times a number is typed )