logo Practice-It logo


Language/Type: Java input method basics Scanner
Author: Roy McElmurry (on 2010/12/28)

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.

Call smallest2(console); smallest2(console); smallest2(console); smallest2(console);
number? 8
number? 10
number? 2
number? 1
number? 22
number? -1
smallest: 1
second smallest: 2
number? 5
number? 6
number? 7
number? 8
number? 9
number? -5
smallest: 5
second smallest: 6
number? 5
number? 5
number? 5
number? 5
number? -3
smallest: 5
second smallest: 5
number? 200
number? 100
number? -103
smallest: 100
second smallest: 200
Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.