Results 1 to 4 of 4
  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Method return with Math.min

    I'm doing selfchecks from my Java learning book.
    In one of them, I'm told to do the following:
    Write a method called min that takes three integers as parameters and returns the smallest of the three values; for example, a call of min(3, -2, 7) would return -2. Use Math.min to write your solution.

    public class SelfCheck_3_12 {
    public static void main(String[] args) {
    int i = 10;
    int j = 20;
    int k = 30;
    int minimum;
    minimum = method(i, j, k);
    System.out.println("minimum is = " + minimum);

    }
    public static int method(int i, int j, int k) {
    int min = Math.min(i, j, k);
    return min;
    }

    }
    Something is wrong with the "int min = Math.min(i, j, k); it seems.
    Anyone care to explain for me? Thanks!

  2. #2
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Method return with Math.min

    Well Math.min can only take two arguments, so you can try doing:
    Java Code:
    int min = Math.min(i, j);
    min = Math.min(min, k);
    :D
    WARNING I am Russian so it's possible that I wont understand you correctly...

  3. #3
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: Method return with Math.min

    Thanks! Actually wouldn't had figured that out easily :D Edit: Confirmed working
    Last edited by LasseA; 10-18-2012 at 11:21 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Method return with Math.min

    If the compiler complains (often with a "can't find symbol") the second thing to check is the API documentation which will tell you the number and types of arguments. This documentation should be open in your browser whenever you code. (Faith based programming just doesn't work)

    The first is typos.

Similar Threads

  1. Cannot return Method's value
    By darshanaprasad in forum New To Java
    Replies: 2
    Last Post: 09-08-2012, 08:25 AM
  2. Math.random() method problem
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-12-2011, 04:50 AM
  3. Using the Math.ceil method
    By Allspark in forum New To Java
    Replies: 8
    Last Post: 09-10-2010, 05:52 AM
  4. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 02:07 PM
  5. Replies: 3
    Last Post: 12-10-2008, 01:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •