Closest to 10

  • 4
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
You can't complete this task, because you're not signed in.
Comments(24)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
HaeWon ChungLevel 5 , Boston
day before yesterday, 04:46
My code doesn't implement the condition that a, b are equally close to 10 but I still pass the task. I think it's because the arguments didn't include the case where a equals b. I'm also a little bit confused with the last condition. Displaying 'either of them' means I should randomize the result? Or is it OK if I choose whatever number I want? For example, if I write a code something like
if (a = b)
System.out.println(a);
Does it meet the last condition?
Mihai Daniel BoneLevel 6 , Bucharest
8 January, 09:10
This one was very confusing, not difficult just very confusing: Tips for the new ones: EX: 9 and 11 are equally close to 10 so just display one of them without any text. To compare the distance between numbers you need to subtract 10 from them; EX: 7 and 14 ........ 7-10= -3 and 14-10=4 so 7 is more close to 10;
Nyerges FerencLevel 13 , Bükkábrány
14 December 2019, 14:29
Well maybe its my English, but "either of them" can mean only one of them or both. A sample might be useful in "Conditions.tcs" for this situation.
JanuszLevel 10 , Radomsko
26 November 2019, 22:49
Both positive and negative values ​​a and b should be considered.
SachinLevel 6 , Pune
22 November 2019, 14:34
use temp variable to hold the result of abs() checked with abs((a-10)) and then compare the both result and return the actual value based on result.
30 September 2019, 17:45
I don't understand why he refuses my solution even though it's good.?
RomanLevel 41
1 October 2019, 07:01
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
MecoSLevel 12
18 September 2019, 17:57
Sorry but i did not understand where is my error. Unsing if, using abs method, print only one number. What else?
RomanLevel 41
19 September 2019, 06:33
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
GáborLevel 14 , Budapest
22 July 2019, 16:55
You can put the abs(here you have the calculation for int a) < abs(calculation for int b ) to the IF condition, and the rest is just printing. :)
6 July 2019, 08:23
I solved the problem (1-hour) but I didn't understand. lol
AbdunabiLevel 4 , Tashkent
11 June 2019, 23:04
who is know? where do I wrong?
public class Solution {
    public static void main(String[] args) {
        displayClosestToTen(8, 11);
        displayClosestToTen(7, 14);
    }

    public static void displayClosestToTen(int a, int b) {
        if(abs(a-10) < abs (b-10))
        {
            System.out.println(a);
        }
        if(abs(a-10) > abs (b-10))
        {
            System.out.println(b);
        }
        if(abs(a-10) == abs (b-10)){
        System.out.println(a+", "+ b);
        }

    }

    public static int abs(int a) {
        if (a < 0) {
            return -a;
        } else {
            return a;
        }
    }
}
RomanLevel 41
12 June 2019, 05:31
Please submit your code for review in the Help section.
Anusha GovindarajanLevel 7 , Amsterdam
16 July 2019, 12:54
You should use else if and else.Please check syntax. if (condition) { } else if(condition) { } else { }