i am feeling problem in writing a code for minimum of two numbres with respect to given code . i need help .

asked 11 Mar '15, 16:14

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311424
accept rate: 0%


Solution { 
        public static int min(int a, int b) { 
           if (a<=b) return a;
             else if(a >b) return b; 
        }

        public static void main(String[] args) throws Exception {
        System.out.println( min(12,33) );
        System.out.println( min(-20,0) );
        System.out.println( min(-10,-20) );
        }
}

Please will refactor your code in the future.

Delete "if(a > b)" in else, stay only "return b" and all will be ok, I think.

link

answered 12 Mar '15, 08:38

RabenokDmitry's gravatar image

RabenokDmitry
1263
accept rate: 14%

i did as you guide me but the result is still same. didnt get the right result .

(12 Mar '15, 15:27) Muhammad Has...

thanks i have done it . we should intialze new valrible in functinos .

(12 Mar '15, 17:25) Muhammad Has...

if (a > b) write b else

link

answered 11 Mar '15, 16:24

Joseph's gravatar image

Joseph
213
accept rate: 0%

yeah did as you said . but broo em feeling problem to code in the given function . should you tell me the proper type .

(11 Mar '15, 16:32) Muhammad Has...

i did this in the following code . but didnt get the result . public static int min(int a, int b) { if(a>b) write b else if(a<b) write a }

(11 Mar '15, 16:35) Muhammad Has...

1) instead of 'write' you need 'return' 2) add ';' at the end 3) what will your code return if a equals b?

(12 Mar '15, 05:09) Elleonora Carry ♦♦

Please write all task and your code when you create your quastion. We donn't know what your programme must do.

If it is method which should return int you must use "return";

if (a > b) 
  return b;
else
  return a;

If it is method which should write result in console and souldn't return result you can use "System.out.println";

if (a > b) 
      System.out.println(b);
    else
      Sysytem.out.prinln(a);
link

answered 12 Mar '15, 07:25

RabenokDmitry's gravatar image

RabenokDmitry
1263
accept rate: 14%

Sorry . Thisis the complete code and i cided in function min(int a, int b) . but didnt get the result . need you help .

import java.lang.System;

/ Minimum of two numbers Write a function that returns a minimum of two numbers. / public class Solution { public static int min(int a, int b) { if (a<b) return="" a;="" else="" if(a="">b) return b; }

public static void main(String[] args) throws Exception
{
    System.out.println( min(12,33) );
    System.out.println( min(-20,0) );
    System.out.println( min(-10,-20) );
}

}

(12 Mar '15, 08:27) Muhammad Has...

Please reread lecture - level 2 lesson 7 "Elly, Function call, return value"

http://javarush.net/levels/level02.html

That lesson contains the example of 'min' method.

link

answered 12 Mar '15, 09:02

Elleonora%20Carry's gravatar image

Elleonora Carry ♦♦
275
accept rate: 16%

  1. public class Solution
  2. {
  3. public static int min(int a, int b)
  4. {
  5. //add your code here
  6. if (a<=b) return a;
  7. else return b;
  8. }
  9. public static void main(String[] args) throws Exception
  10. {
  11. System.out.println( min(12,33) );
  12. System.out.println( min(-20,0) );
  13. System.out.println( min(-10,-20) );
  14. }
  15. }
link
This answer is marked "community wiki".

answered 28 Oct '17, 10:39

Winnermann's gravatar image

Winnermann
111
accept rate: 0%

edited 28 Oct '17, 10:44

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×53
×31
×17

Asked: 11 Mar '15, 16:14

Seen: 3,066 times

Last updated: 28 Oct '17, 10:44