以下是代码:
public class JavaApplication3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter number of trains");
int n = input.nextInt();
int minmin;
for (int i = 0; i < n; i++) {
System.out.println("enter hour of arrival");
int hour = input.nextInt();
System.out.println("enter minute of arrival");
int min = input.nextInt();
System.out.println("how many minutes will it stay in the station?");
int stay = input.nextInt();
minmin = min + stay;
}
}
}
我想比较每列火车的时间。我尝试用数组做它,但它充满了相同的元素。什么是更好的方法呢?
答案 0 :(得分:0)
class Train {
int hourOfArrival;
int minuteOfArrival;
int stay;
int hourOfDeparture;
int minuteOfDeparture;
// int order;
public Train(int hourOfArrival, int minuteOfArrival, int stay) {
this.hourOfArrival = hourOfArrival;
this.minuteOfArrival = minuteOfArrival;
this.stay = stay;
this.minuteOfDeparture = (minuteOfArrival + stay) % 60;
this.hourOfDeparture = hourOfArrival + ((minuteOfArrival + stay) / 60);
}
}
public class Demo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter number of trains");
int n = input.nextInt();
Train[] trains = new Train[n];
for (int i = 0; i < n; i++) {
System.out.println("enter hour of arrival");
int hour = input.nextInt();
System.out.println("enter minute of arrival");
int min = input.nextInt();
System.out.println("how many minutes will it stay in the station?");
int stay = input.nextInt();
trains[i] = new Train(hour, min, stay);
// your logic to compare the train departure time
}
// your logic to compare the train departure time
}
}
我认为上面的代码会为您提供足够的数据来比较每列火车的出发时间,并获得将提前离开平台的火车,或者您可以找到他们离开平台的顺序。 我已经离开了比较出发时间的逻辑。你可以尝试那部分。;)