It's not a secret anymore!
The moose likes Beginning Java and the fly likes Largest and smallest number Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Largest and smallest number" Watch "Largest and smallest number" New topic
Author

Largest and smallest number

wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
Hi everyone. I am trying to write a java program that allows the user to input as many integer values. At the end, it is supposed to figure out which is the largest and smallest number. -99 is used to quit if the user no longer wants to put in anymore values. Also, if -99 is entered in the first time, it will say "You did not enter any numbers". I wrote this code but am having a hard time getting it figure out which is the smallest.





Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14685
    
  16

What do you want to express with "if (input != min) " ?


[My Blog]
All roads lead to JavaRanch
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
i was just messing around. i'm a beginner to java. it should be "<"
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14685
    
  16

Are you sure that initializing min and max to 0 will not cause any problem ?
Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Be sure to use the code button. It makes it easier to read.

Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Can't you just do the following?

Instead of


try this:
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
thank you for your reply!

i tried it and it still doesn't work.

i have a question about initializing min, max, and input. do i have to put int min(max,input) = 0? i feel that THAT is the problem but if i don't set it to a value it won't compile
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14685
    
  16

There are several ways to initialize them :
1. Use Integer.MIN_VALUE and Integer.MAX_VALUE
2. Or, the first time the user inputs a value, initialize both min and max to that value
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
how do you the 2nd? (first time the user inputs a value, initialize both min and max to that value)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14685
    
  16

After the first "input = keyboard.nextInt();", set both min and max to the input value.
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
cheese and rice it works!! thank you!!
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

Jacob Coddaire wrote:try this:

min = min? Why would you ever want to assign a variable to itself?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Largest and smallest number
 
Similar Threads
How to write an algorithm for an average of values?
How to "throw an exception?"
passing to method and returning value from method
input number to array --not displaying right item [code included]
Help! I need to convert a string arry into double array