In conditions say:
Write the displayClosestToTen method. The method should display the argument that is nearest to 10.
For example, given the numbers 8 and 11, 11 is closest to ten. If both numbers are equally close to 10, then display either of them.
I wrote the code for the underlined part ( verify won`t pass).
But when I put comment in that part of code (verify is passed).
Everything is OK but Verify does not confirm this
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Okanlawon Oluwatobi Damilare
15 March 2019, 20:55
public static void displayClosestToTen(int a, int b) {
// write your code here
if(abs(a-10) < abs(b-10) ){
System.out.println(a);
}else
System.out.println(b);
}
+4
Minh Phạm Đức
27 February 2019, 02:45
you don't need
if(abs(a-10) == abs(b-10))
System.out.println(a+" = "+b);
because it falls into the else case of if (abs(a-10) < abs(b-10))
in this case, the program will display b as a result and hence matches with the condition of " If both numbers are equally close to 10, then display either of them."
Edit: if you want to retain the code then
change it to
else if (abs(a-10) == abs(b-10))
//System.out.println(a);
//System.out.println(b);
uncomment one of the line above because you're are displaying either, not both... (but then it's not necessary to have this code block...)
0
Milan
27 February 2019, 08:20
Ok,
but i coment part of my code and my code(verify is passed).
// if(abs(a-10) == abs(b-10))
and this condition is required
0
Minh Phạm Đức
27 February 2019, 10:44
well, the requirement is display either a or b
in your code, b is always displayed in case both numbers are equally close to 10
so that code block is not needed.
If you still want to have a separate code block for it then change the if to "else if" and move it up above the "else" statement and print out a or b as you please.
0
Milan
27 February 2019, 11:13
I`ts true. My code did not meet this requirement.
However, he passed the verification regardless of the fact that he did not fulfill this condition.
when I typed // at the beginning of this line: if(abs(a-10) == abs(b-10))
like a wrote in my frist comment: "But when I put comment in that part of code (verify is passed)."
0
Minh Phạm Đức
28 February 2019, 00:44
well, without the if(abs(a-10) == abs(b-10)) your code still meets the requirement (that's why it passed the verification)
as it falls into the case of the else clause (your else clause indicates the case where abs(a-10) >= abs(b-10)) which has already had the abs(a-10) == abs(b-10)) as a part of it). And the returned result for that else clause is always b in your code which satisfies the "display either a or b" <.strong>condition.
Remember either a or b, meaning you only need one: a printed out or b printed out not both.
0
Milan
26 February 2019, 22:05
I wrote the code as required in the conditions.
and did not pass the confirmation. My code:
package com.codegym.task.task04.task0409;
/*
Closest to 10
*/
public class Solution {
public static void main(String[] args) { //The main method should call the displayClosestToTen method.
displayClosestToTen(8, 11); //The main method should not call System.out.println or System.out.print().
displayClosestToTen(7, 14); //The main method should not call System.out.println or System.out.print().
// displayClosestToTen(20,0); //The main method should call the displayClosestToTen method.
} // If both numbers are equally close to 10, then display either of them.
public static void displayClosestToTen(int a, int b) { //The displayClosestToTen method should call the abs method.
// write your code here
if (abs(a-10) < abs(b-10)){
System.out.println(/*"Broj a "+*/a/*+" je blizi broju 10 razlika je " + abs(a - 10)*/); // The program should display text on the screen.
} else {
System.out.println(/*"Broj b "+*/b/*+" je blizi broju 10 razlika je " + abs(b - 10)*/); // The program should display text on the screen.
} //The displayClosestToTen method should display a number on the screen in accordance with the task conditions.
if(abs(a-10) == abs(b-10))
System.out.println(a+" = "+b);
//If both numbers are equally close to 10, then display either of them.*/
}
public static int abs(int a) { // metod convert number in absolute number
if (a < 0) {
return -a;
} else {
return a;
}
}
}
0
Khurram
26 February 2019, 13:48
could you share the code and the error(s) that you are receiving
0