Hi! I'm trying to create a program that will allow the user to input 5 numbers
and will print the largest and smallest number.

I tried doing it and this is what I came up.. The thing is, after the program executes its purpose i want to add a statement that will allow you to loop the program or ends it.
I'm having a hard time trying to figure out where to put the looping statement and what kind. Can anyone post your suggestion..

and also if you have any suggestion in what to do to simplify this program your welcome to do so :)

import java.util.Scanner;
public class LargestAndSmallestNumber {
    
    
    public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
        // Variable Declaration
        float num;  //variable to hold the current number
        float max;  //variable to hold the largest number
        float min;  //variable to hold the smallest number
        int count;  //loop control variable
        
   
        System.out.println("This program accepts 5 input numbers and will" +
                  " print the highest and lowest number.");
        System.out.print("Enter 5 integers: ");
        num = console.nextFloat();
        
         max = num;
         min = num;
            for (count = 1; count < 5; count++) 
            {
              num = console.nextFloat();
              max = larger(max,num); 
              min = smaller(min,num);
             }//end of for statement
         System.out.println("******************************");
         System.out.println("The largest number is " + max);
         System.out.println("The smallest number is " + min);
         System.out.println("******************************");
         
    }

 
    public static float larger(float x, float y)
    {
        float max;
        if(x >= y)
            max = x;
        else
            max = y;
        return max;
    }//end of larger method
       public static float smaller(float a, float b)
    {
        float min;
        if(a <= b)
            min = a;
        else
            min = b;
        return min;
    }//end of smaller method
       
}

A simple solution would be to enclose everything in a do while loop:

Like this:

do{
//stuff to repeat
//
//
//
System.out.printn("Do again? [Y/N]");
choice=console.next();
} while(choice.trim().equalsIgnoreCase("Y");

I've been trying to figure out how to put the code you posted here and ends up having too many errors..

A simple solution would be to enclose everything in a do while loop:

Like this:
Java Syntax (Toggle Plain Text)
do{
//stuff to repeat
//
//
//
System.out.printn("Do again? [Y/N]");
choice=console.next();
} while(choice.trim().equalsIgnoreCase("Y");

in Line 7 you put

choice=console.next();

so it is a string variable? why do I need to put a string if the choices are only Y and N?? ..

in Line 8 .. I never encountered this kind of code

choice.trim().equalsIgnoreCase

what does the code .trim() do to the program?

First off what do u mean errors??

And to answer your first doubt...

Well, I dont think there is a way to "truely" read a character from the command line. You can ofcourse read byte and cast to char. But thats all confusing and unnecessary.

Thats why I went for the String choice. Dude come on... "Y" and "N" are strings. You know what? "" is also a string. and if you are so so precise of using characters while comparison... you can do

Character ch=choice.charAt(0);

So no problem with that. What ever user types is stored as a string and then u can compare it with the string "Y".

Second doubt:

The trim() method actually removes white spaces surrounding a word. So to prevent white spaces infront of the read string to cause problems while comparison I used trim before doing .equalsIgnoreCase(). Its a good practice to use trim().


BUT BUT BUT! On second thought console.next() automatically ignores those spaces and reads the string alone....

So trim wont make any difference. Your choice... have it or drop it.

:D orayt!!! I'll try doing it again :) thanks for the explanation. I forgot that "" is also considered as String (I suck!)

Hopefully this time I can do it...

} while(choice.trim().equalsIgnoreCase("Y");

:D HAHA! You're right!!! thank you so much stevanity, there is just one simple mistake that I didn't notice probably just a typo error..

} while(choice.trim().equalsIgnoreCase("Y"));

it's the one in highlight!! XD omg you made me reaaally happy!!!!! again, thank you so much..

lol. sorry for that...
glad ure happy! nothing gives more joy than when u get ure code right.

:D there's just one more thing I forgot before posting here.. haha now someone might copy my work (Classmates) for our assignment.

sigh! wrong title, I guess :D I should have not made it too easy to spot.

This article has been dead for over six months. Start a new discussion instead.