Hello! Tell me on the instructions whether the code is correct or what can be added or simplified?

Create a program that displays the nearest and least to 10 of the two numbers written in the variables m and n.

public class Test { public static void main(String args[]) { int n = 11; int m = 7; if (n < m) { System.out.println("Π‘Π»ΠΈΠΆΠ°ΠΉΡ‰Π΅Π΅ число ΠΊ 10 = " + m); } else { System.out.println("Π‘Π»ΠΈΠΆΠ°ΠΉΡˆΠ΅Π΅ число ΠΊ 10 = " + n); } if (n > m) { System.out.println("НаимСньшСС число ΠΊ 10 = " + m); } else { System.out.println("НаимСньшСС число ΠΊ 10 = " + n); } } } 

    2 answers 2

    In this case, the nearest one thinks correctly, but if m = 5 and n = 3, then he considers it wrong. I would think so somewhere (pseudocode)

     if (abs(10-m) < abs(10-n)) 'm Π±Π»ΠΈΠΆΠ΅' else 'n Π±Π»ΠΈΠΆΠ΅'. abs находится Π² java.lang.Math 

    But what is the "lowest number to 10" - I do not know.

      You can put System.out.print ("The nearest number to 10 =") before the if-else block, and leave only System.out.println (m) inside. Well, I agree with the answer above.

      What is the "Lowest Number to 10"?

      • Sorry, the error came out of another task. The smallest number - turtles