Steve Shaydy
Steve Shaydy

Reputation: 11

How to make my java program print the smallest and largest numbers from the values i input

I need java to ask for an int type input and then print it out immediately after and keep asking for numbers until a sentinel is entered to tell the program to stop asking for numbers. Once the program stops asking for numbers, I need it to print out the smallest number and the largest number out of that set of numbers I've typed in, however I cannot use an array or anything of that sort because I need to make a method to execute this operation. I have only been doing java for 2-3 months now so I'm basically a beginner.

Upvotes: 0

Views: 1032

Answers (1)

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

Sounds like a homework. So would not give you the exact solution. But you can try something like this:

 //declare two variables
 private static int max = 0;
 private static int min = 0;

Now inside your main method as you keep entering the integers you can compare and replace the values

  if (yourEnteredVariable > max){
      max =  yourEnteredVariable;
  }

  if (yourEnteredVariable < min){
     min =  yourEnteredVariable;
  }

You can have the above clauses in separate methods.

Upvotes: 2

Related Questions