Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Math.max function
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Sat May 13, 2006 9:16 pm   Post subject: Math.max function

i'm trying to figure out the largest number out of 5 numbers user entered. i made the program but i don't really understand the function Math.max(). how does it work? and wat stuff goes in the brackets? if your interested in looking at my program here:

code:

import java.io.*;

public class Largest_Number{
    public static void main(String[] args){
        String snum;
        int inum;
        int sum = 0;
        int m = -999999999;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for (int i = 1; i<=5; i++){
            System.out.print("enter number " + i + " of 5: ");
            snum = in.readLine();
            inum = Integer.parseInt(snum);
            m = Math.max(m,inum);
        }
        System.out.println("\nand the largest is " + m);
        }catch(IOException io){
            System.out.println("error reading input");
        }catch(NumberFormatException nfe){
            System.out.println("can't convert");
        }
    }
}
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Sat May 13, 2006 11:09 pm   Post subject: (No subject)

I don't know Java, but are you saying the Math.Max function isn't working the way you want it to or that you don't know what it does? Isn't is similar to Turing's max function?
HellblazerX




PostPosted: Sun May 14, 2006 10:27 am   Post subject: (No subject)

There's nothing wrong with your code. It works just fine. You used the Math.max method just fine, so I'm not sure why you're asking how to use it. Math.max just returns the largest of two numbers you entered as the parameters. I guess it just uses a simple if statement to determine which number is higher, but this method seems a bit unneccesary, seeing as you can just use this as well:
code:
m = (m > inum ? m : inum);
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: