我正在研究以下问题:
A time by the 24-hour clock is a 4-digit number where the leftmost two
digits denote the hour (0 to 23, incl.) and the rightmost two digits
denote the minutes (0 to 59, incl.). For example, 2130 expresses
half-past nine in the evening. Write a class Time to encapsulate a
time. It should have the following instance methods:
get to read a time from the keyboard (supplied as a four-digit number
e.g. 2130). You may assume the number represents a valid time.
到目前为止我的代码是:
import java.util.Scanner;
import java.io.*;
class Time {
private double hour, min;
Scanner scanner = new Scanner(System.in);
Time() {
hour = 00;
min = 00;
}
Time(double h, double m) {
hour = h;
min = m;
}
void get() {
System.out.println("Please enter a time in 24 hour format: ");
double x = scanner.nextDouble();
hour = x / 100;
min = x % 100;
System.out.println("The time is " + hour + ":" + min);
}
public String toString() {
return "Time: " + hour + min;
}
}
我的问题是如何将4位数输入分成小时和分钟,所有建议,提示赞赏.