Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing java/04-stdin/MaxMin by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Write a program <code>MaxMin</code> that reads in an arbitrary
number of integers from standard input, and prints out the
minimum and maximum values
Public permissions remove
Engine
Template / Reference solution
 
public class MaxMin {
   public static void main(String[] args) {
    
      // first value read initializes min and max
      int max =\[ StdIn.readInt() ]\;
      int min =\[ max ]\;
    
      // read in the data, keep track of min and max
      while (\[!StdIn.isEmpty()]\) {
         int value = StdIn.readInt();
\[
         min = Math.min(min, value);
         max = Math.max(max, value);
]\
      }
    
      // output
      StdOut.println("max = " + max + "   min = " + min);
   }
}
Java test suite
See manual
 
stdin = "23 45 17 56 32\n89 10 53 32 34\n16";
testMain();
stdin = "1 2 3 4 5";
testMain();
stdin = "-1 -2 -3 -4 -5";
testMain();
stdin = "126";
testMain();
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.