#code that calculates the smallest of three numbers. Rewrite: instead of ? and : use if else

7 messages · Page 1 of 1 (latest)

raven hill
#

Write a function that calculates the smallest of three numbers.

Hint:
You must write the body of the existing function min.

Requirements:
The program must display text on the screen.
The method min must not display any text on the screen.
The method main must call the method min four times.
The main method must display the result of the min method. The record should be on a separate line each time.
The min method must return the smallest number from the numbers a, b and c.

with the given code(screenshot)

I tried rewriting it on my own

public class Solution {
public static int min(int a, int b, int c) {
//write your code here makes no sense as b<c unnecessary (a < b && b < c)
int m1;
if (a<b)
return a;
else
return b;
b= m1;
// b is smaller than a but it is also smaller than c
// ? a was not tested against c now
if (m1< c)
return m1;
else
return c;

}

public static void main(String[] args) throws Exception {
    System.out.println(min(1, 2, 3));
    System.out.println(min(-1, -2, -3));
    System.out.println(min(3, 5, 3));
    System.out.println(min(5, 5, 10));
}

}

but that didn´t work out

I would really appreciate it if someone could rewrite the following solution for me with if and else which is apparently a replacement for the symbols " ? and : "

sly pivotBOT
#

This post has been reserved for your question.

Hey @raven hill! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

sly pivotBOT
raven hill
#

code that calculates the smallest of three numbers. Rewrite: instead of ? and : use if else

subtle breach
#
public static int min(int a, int b, int c) {
  return Math.min(a, Math.min(b,c));
}
pure remnant