smallest2

Category: Programming
Author: Victoria Kirst
Book Chapter: 5.4
Problem: smallest2
Write a static method named smallest2 that accepts a Scanner for console input as a parameter. The method repeatedly prompts the user for a sequence of integers until the user enters a negative number. The method then prints the smallest two nonnegative numbers entered by the user. (You may assume the user will enter at least 2 nonnegative numbers.) Notice you do not have to print the two smallest unique numbers entered by the user. For example, if the user enters 2, 2, and 3, the two smallest numbers entered are 2 and 2. Here are some example calls to the method and their resulting console output (user input is bolded and underlined).

Here are some example calls to the method and their resulting console output (user input is bolded and underlined). Assume a Scanner named console was initialized earlier in the code before each method call.

Call on smallest2(console);
	number? 8
	number? 10
	number? 2
	number? 1
	number? 22
	number? -1
	smallest: 1
	second smallest: 2

Call on smallest2(console);
	number? 5
	number? 6
	number? 7
	number? 8
	number? 9
	number? -5
	smallest: 5
	second smallest: 6

Call on smallest2(console);
	number? 5
	number? 5
	number? 5
	number? 5
	number? -3
	smallest: 5
	second smallest: 5

Call on smallest2(console);
	number? 200
	number? 100
	number? -103
	smallest: 100
	second smallest:
	200