G
G
Good Samaritan2019-02-11 12:50:15
Java
Good Samaritan, 2019-02-11 12:50:15

Strangeness when calculating the number of minutes / seconds, why is that?

private void runTimer() {
 final TextView timeView = (TextView)findViewById(R.id.time_view);
 
 int hours = seconds/3600;
 int minutes = (seconds%3600)/60;
 int secs = seconds%60;
 String time = String.format(Locale.getDefault(),
 "%d:%02d:%02d", hours, minutes, secs);
 timeView.setText(time);
 if (running) {
 seconds++;
 }
 
}

Everything is clear about hours.
Why write minutes=(seconds%3600)/60;, isn't minutes just seconds/60?
And why in a variable that stores seconds, write seconds%60? Aren't seconds in seconds? Let's say seconds is 60, then it turns out that sec=0 .
What is the meaning of this? Explain, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2019-02-11
@djamali

% - remainder of the division .
101% 60 = 41 seconds.
minutes otherwise would have to be written more difficult
(seconds-hours * 3600) / 60
3664 seconds is 1 hour 1 minute 4 seconds.
and according to you it turns out 1 hour 61 minutes 3664 seconds

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question