Hi all,
i am new to java and this forum. I am hoping i can get some help with the below program:
Write a program that reads integers, finds the smallest of them and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 6 2 5 2 2 3 0; the program finds that the smallest is 2 and the occurrence count for 2 is 3.
Hint: maintain two variables min and count. Min stores the current min number and count stores its occurrences. Initially assign the first number to min and 1 to count. Compare each subsequent number with min. If the number is less than min, assign it to min and reset count to 1. If the number is equal to min increment count to 1.
Below is what i have started:
import java.util.Scanner;
public class Integers {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
int times = 1;
int count = 1;
int min; // The smallest of the integers entered
int minNumber = 6;
// Read an initial data
System.out.print(
"Enter an int value (the program exits if the input is 0): ");
int data = input.nextInt();
int minimum = data;
// Keep reading data until the input is 0
int sum = 0;
while (data != 0) {
sum += data;
// Read the next data
System.out.print(
"Enter an int value (the program exits if the input is 0): ");
data = input.nextInt();
}
//i am not sure where in the program to put in the check for each number against the min number and reset the count as per question above??
if (data < minNumber)
{
minNumber = data;
}
//keep getting 0 for smallest number but do not want to include the 0 as this is just for exiting the input of integers??
System.out.printf( "Smallest Number is %d\n", minNumber );
//not sure if below correct either but i think i am close!!!
for (int i = 0; i < 100; i++)
count++;
count++;
if (minNumber > 1){times++;}
System.out.println("Number " + minNumber + " occurs " + times + " Times ");
}
}