/* I'm trying to compile this program, but cant get it to run smoothly. When it prompts you how many integers should I compare? if I put 5 for example... I can only go up to 5 and will display the smallest number between 0 and 5. But what if I want to input a number larger than 5 after I said "how many integers should I compare?" lets see a 20. If I do that it will give me an "Invalid Input!". Also If I put a letter, when it says enter value 0: G ... it crashes, but I put an else condition that says System.out.println("Invalid input!"); Thanks */
import java.util.Scanner;
public class SmallestInt {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);//create object to obtain input
int ii;
int smallest = Integer.MAX_VALUE;//stores smallest integer
int number = 0;
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
int initialValue = input.nextInt();
for(ii = 0; ii <= initialValue; ii++)
{
System.out.print("Enter value " + ii + ": ");
number = input.nextInt();
if(number < smallest)
{
smallest = number;
}
else
System.out.println("Invalid input!");
}//end for
System.out.printf("The smallest integer is: %d\n", smallest);
}//end main
}//end class Smallest