Issue
My logic seems to work for every one of the examples, but when I try to submit it, it comes up as wrong because there is one test input (which is not revealed) that somehow results in my code spitting out "24hours and 10min" which is wrong and that the answer should be "0hours and 10min".
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int xminutes = sc.nextInt();
int y = sc.nextInt();
int yminutes = sc.nextInt();
int xm = x\*60 + xminutes;
if (y\<=x)y+=24;
int ym = y\*60 + yminutes;
System.out.println("O JOGO DUROU "+((ym-xm)/60)+" HORA(S) E "+ ((ym-xm)%60) +" MINUTO(S)");
}
}
Solution
Let's assume the times are begin=9:10 end=9:20. The correct answer is 10 minutes, obviously.
Then x = y = 9 and you execute if (y<=x)y+=24
, which is where the extra 24 comes from.
You need to consider the whole times (xm and ym) in order to decide whether the 'end' is the next day after 'begin', and you must add 24 hours (or 24 * 60 minutes).
Answered By - access violation
Answer Checked By - Mildred Charles (JavaFixing Admin)