1

this is my first coding class ever and i'm very new to all this please. I'm trying to create a code that will accept two numbers and return the larger of the two numbers. For e.g if the function is given da integers 7 and 12 the function will return da integer 12. This is the code i've written so far but it's far from being correct.

 public class Return
    {
        public static void main(String[] args) 
        {
           public static int max("int num1, int num2,");
              int result;
              if (num1 > num2)
                result = num1;
              else
                result = num2;
            return result;

            }
        }
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Jcole
  • 29
  • 1
  • 3
  • 2
    You have to move the `max` method outside the `main()` method. And parameters should be like `max(int num1, int num2)` followed by a opening curly brace – TheLostMind Oct 29 '15 at 06:11
  • Since you are a beginner, I would suggest you to do some research before posting your questions directly here. – amit28 Oct 29 '15 at 07:10

4 Answers4

4

Java do not have nested methods. You write method inside method. Move your method outside and there are some syntax errors

public class Return
{
public static void main(String[] args) 
{
   int result = max (3,4);
   System.out.println(result);

}

    public static int max(int num1, int num2){
        int result;
       if (num1 > num2)     
         result = num1;
       else
         result = num2;
        return result;

        }
}
Avinash A
  • 91
  • 5
1

I suggest you should read some basic concepts of programming and language you are using. But let me try to help you. Your code should look something like:

public static int max(int num1, int num2) {
        int result;
        if (num1 > num2)
            result = num1;
        else
            result = num2;
        return result;
    }

    public static void main(String[] args) {
        System.out.println(max(1, 2));

    }

Mistakes in your code were:

  1. max method declared inside main.
  2. arguments are passed inside quotes "int num1, int num2" which is wrong.
  3. No definition of max
  4. not calling max from main

I hope it helped to understand issues with the code.

codingenious
  • 8,385
  • 12
  • 60
  • 90
1

This short code will return a larger one from two integers.

public static int larger(int a, int b)
{
    return a >= b ? a : b; 
}

Copy paste this method to your desired class and call this method

larger(12, 7);

Given your class:

public class Return
{
    public static int larger(int a, int b)
    {
        return a >= b ? a : b; 
    }

    public static void main(String[] args) 
    {      
        int larger127 = larger(12,7);
        System.out.println("The larger int from 12 and 7 is: " + larger127);
    }
}
0

You cannot have a method inside another method. Do it like this:

public static void main(String[] args) {        
    int result = max (3,4);
    System.out.println(result);
}

private static int max(int i, int j) {
    return i > j ? i : j; 
}

max uses ternary operator to find the largest of the two numbers.

Community
  • 1
  • 1
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23