Электронные часы показывают время в формате h:mm:ss, то есть сначала записывается количество часов, потом обязательно двузначное количество минут, затем обязательно двузначное количество секунд. Количество минут и секунд при необходимости дополняются до двузначного числа нулями.
С начала суток прошло n секунд. Выведите, что покажут часы.
Входные данные
Вводится целое число n.
Выходные данные
Выведите ответ на задачу, соблюдая требуемый формат.
For example:
InputResult
3602 1:00:02
129700 12:01:40
______________________________________
Решил её так:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int h = ((n / 60) / 60) % 24;
int m = n / 60 % 60;
int s = n % 60;
int s1 = (s / 10);
int s2 = (s % 10);
s = s1 + s2;
System.out.println(h + ":" + m + ":" + s);
}
}
Но нет 0 т. е вместо 1:00:02 пишет 1:0:2
JavaScript
Помогите решить задачу на языке java
Вместо
System.out.println(h + ":" + m + ":" + s);
Написать:
System.out.print(h + ":");
if (m<10) System.out.print("0");
System.out.print(m + ":");
if (s<10) System.out.print("0");
System.out.println(s);
Ну хотябы так!
System.out.println(h + ":" + m + ":" + s);
Написать:
System.out.print(h + ":");
if (m<10) System.out.print("0");
System.out.print(m + ":");
if (s<10) System.out.print("0");
System.out.println(s);
Ну хотябы так!
Хотите сказать что 129700 секунд это 12 часов? Вообще-то в сутках 86400 секунд, а 129700 это 36 часов
Есть куча классов и методов работы со временем. Вам нужно отформатировать n секунд (или n*1000 миллисекунд ) и представить строкой- временем.
Достаточно только ввести n и выдать строку.
смотрите например https://urvanov.ru/2016/06/16/java-8-дата-и-время/
Достаточно только ввести n и выдать строку.
смотрите например https://urvanov.ru/2016/06/16/java-8-дата-и-время/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
if (a < 86400) {
int h = ((a / 60) / 60);
int m = ((a / 60) % 60);
int s = (a % 60);
if (m > 9) {
if (s > 9)
System.out.println(h + ":" + m + ":" + s);
else
System.out.println(h + ":" + m + ":" + "0" + s);
} else {
if (s > 9)
System.out.println(h + ":" + "0" + m + ":" + s);
else
System.out.println(h + ":" + "0" + m + ":" + "0" + s);
}
} else {
int h = (((a / 60) / 60) % 24);
int m = ((a / 60) % 60);
int s = (a % 60);
if (m > 9) {
if (s > 9)
System.out.println(h + ":" + m + ":" + s);
else
System.out.println(h + ":" + m + ":" + "0" + s);
} else {
if (s > 9)
System.out.println(h + ":" + "0" + m + ":" + s);
else
System.out.println(h + ":" + "0" + m + ":" + "0" + s);
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
if (a < 86400) {
int h = ((a / 60) / 60);
int m = ((a / 60) % 60);
int s = (a % 60);
if (m > 9) {
if (s > 9)
System.out.println(h + ":" + m + ":" + s);
else
System.out.println(h + ":" + m + ":" + "0" + s);
} else {
if (s > 9)
System.out.println(h + ":" + "0" + m + ":" + s);
else
System.out.println(h + ":" + "0" + m + ":" + "0" + s);
}
} else {
int h = (((a / 60) / 60) % 24);
int m = ((a / 60) % 60);
int s = (a % 60);
if (m > 9) {
if (s > 9)
System.out.println(h + ":" + m + ":" + s);
else
System.out.println(h + ":" + m + ":" + "0" + s);
} else {
if (s > 9)
System.out.println(h + ":" + "0" + m + ":" + s);
else
System.out.println(h + ":" + "0" + m + ":" + "0" + s);
}
}
}
}
import java.util.Scanner;
public class timer {
public static void main(String[] args) {
int second60, minutes, minutes60, hours, hour24;
Scanner sc = new Scanner(System.in);
int seconds = sc.nextInt();
second60 = seconds%60;
minutes = seconds/60;
minutes60 = minutes%60;
hours = seconds/3600;
hour24 = hours%24;
System.out.print(hour24 + ":");
if (minutes60<10) {
System.out.print("0" + minutes60 + ":");
}
else {
System.out.print(minutes60 + ":");
}
if (second60<10) {
System.out.print("0" + second60);
}
else {
System.out.print(second60);
}
}
}
public class timer {
public static void main(String[] args) {
int second60, minutes, minutes60, hours, hour24;
Scanner sc = new Scanner(System.in);
int seconds = sc.nextInt();
second60 = seconds%60;
minutes = seconds/60;
minutes60 = minutes%60;
hours = seconds/3600;
hour24 = hours%24;
System.out.print(hour24 + ":");
if (minutes60<10) {
System.out.print("0" + minutes60 + ":");
}
else {
System.out.print(minutes60 + ":");
}
if (second60<10) {
System.out.print("0" + second60);
}
else {
System.out.print(second60);
}
}
}
Похожие вопросы
- Помогите решить задачу
- помогите решить задачу по javascript
- Помогите решить задачи в Javascript:
- Помогите решить задачу по js! Программа выводит все заглавные буквы в тексте. Как распределить эти буквы по алфавиту?
- Помогите решить задачу в JavaScript, тема: матрицы.
- нужно решить задач с помощь js ...
- Не могу решить задачу по JS
- Помогите доделать задачу используя функции
- Помогите с задачей по js
- Помогите решить задачку по js