Quick question about this simple code..
Hello everyone. I was to create a code that finds the smallest number, assuming that the user first enters the amount of numbers to be input. I thought this would be a simple task but i ran into a little issue with it. Here is my code, the problem is that the program keeps saying that the last number input is the smallest...where did i go wrong?
Code:
import java.util.Scanner;
public class SmallestValue {
public static void main(String[] args) {
int smallest=999999999;
int number;
int counternum;
int counter=1;
Scanner input=new Scanner (System.in);
System.out.println("Please the number of values to be input: ");
counternum=input.nextInt();
System.out.println("Enter a number ");
number=input.nextInt();
number=smallest;
counter++;
while (counter <= counternum){
System.out.println("Enter another number: ");
number=input.nextInt();
if (number<smallest)
smallest=number;
counter++;
}
System.out.println(counternum + " numbers entered.");
System.out.println("The smallest number entered was "+ number);
}
}