Skip to content

Instantly share code, notes, and snippets.

@SysOverdrive
Created March 4, 2016 13:27
Show Gist options
  • Save SysOverdrive/4b7b837337c37e252ebe to your computer and use it in GitHub Desktop.
Save SysOverdrive/4b7b837337c37e252ebe to your computer and use it in GitHub Desktop.
//Write a class called MinMaxPrinter. Using a Scanner, ask the user to enter
//a series of integers and find the maximum and the minimum value.
//Use a loop.
//Do not let bad input (a non-integer) terminate your program with an error.
//When the user enters any non-integer, print the maximum and minimun values
//on separate line and quit.
//If the numbers entered are 5 10 3 8 1, 2 9, the output will be
//10
//1
//HINT: remember hasNextInt()
//HINT: initialize max to Integer.MIN_INTEGER rather than to 0 and min to
// Integer.MAX_INTEGER before the loop.
// Need help starting this question? In the lesson titled
// "Starting points: Problem Set Questions", go to the
// problem titled "Problem Set 4 - Question 3" for some tips on
// how to begin.
import java.util.Scanner;
public class MaxMinPrinter
{
public static void main(String[] args)
{
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
System.out.print("Enter an integer: ");
for (Scanner in = new Scanner(System.in); in.hasNextInt(); )
{
System.out.print("Enter an integer: ");
int number = in.nextInt();
if (max < number) max = number;
if (min > number) min = number;
}
System.out.println(max);
System.out.println(min);
//your code here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment