r/learnjava • u/UpriseDaGr8 • 2h ago
What is wrong with this code? MOOC.FI
So this piece of code is for finding the absolute value:
import java.util.Scanner;
public class AbsoluteValue {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = Integer.valueOf(scanner.nextLine());
if(num < 0){
num = (num*(-1));
}
System.out.println(num);
}
}But TMC MOOC.FI is showing this error:
TestMyCode
Some tests failed on the server
Need help?
Points: 0.00%
...
x
Test passed!
AbsoluteValueTest absolute ValueOfNegativeThree
Test passed!
AbsoluteValueTest absoluteValueOfSix
1
Upvotes
View all comments
Show parent comment
•
u/T_Caf 36m ago
Understandable, and I see where you're going, kudos for that. I think it is just a bit difficult to get the absolute value,for negative integer, to print because that value does not appear to reach the print statement. May I suggest a second System.out.println within the if code block?