Computer Science Canada

Math.max function

Author:  cool dude [ 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");
        }
    }
}

Author:  NikG [ Sat May 13, 2006 11:09 pm ]
Post 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?

Author:  HellblazerX [ Sun May 14, 2006 10:27 am ]
Post 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);


: