英文:
Find out if a specific time frame is free in an array
问题
这个“飞行俱乐部”项目旨在帮助飞行俱乐部个人管理他们的工作,因此有几种方法可以实现这个目的。然而,我是一个初级的 Java 开发者,并不太熟悉 Java 的灵活性及其在处理一些稍微困难的任务时的功能,比如下面这个任务:这个方法应该检查在特定的时间范围内(由用户指定)是否有任何航班空闲,然后打印出那些实际上是空闲的航班,如果没有,它应该打印出(很遗憾,这个时间段内没有可以预订的航班)。我尝试了几种不同的方式和方法,但是它们都不起作用:
方法:
public static void rentFlight(int[] flightQuantity, String[] identification, String[] time, int index, boolean free) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("您想租用哪个航班(请输入航班编号)");
index = sc.nextInt();
System.out.println("您打算什么时候租用航班:(hh:mm)");
String z = sc.nextLine(); // 给定的时间段
for (int i = 0; i < time.length; i++) { // 遍历时间数组
if (!z.contains(String.valueOf(time))) { // 如果给定的时间与任何方式都不匹配,则航班是空闲的
System.out.println("该航班是空闲的,可以租用:" + identification[i]);
} else {
System.out.println("很遗憾,没有空闲航班");
break;
}
}
}
将参数填充为用户输入的方法:
public static void rentAll(int[] flightQuantity, String[] identification, String[] time, String[] name, String[] surname, int index, boolean free) throws ParseException {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < flightQuantity.length; i++) {
System.out.println("航班编号:" + flugAnzahl[i]);
index++;
System.out.println("航班描述:" + bezeichnung[i]);
System.out.println("您想在什么时间租用航班");
System.out.println("请输入时间(格式为:hh:mm)");
time[i] = sc.nextLine();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm");
Date time2 = ft.parse(time[i]);
System.out.println(ft.format(time2));
Date dJetzt = new Date();
System.out.println("当前时间是:" + ft.format(dJetzt));
System.out.println("个人的名字:");
name[i + 1] = sc.nextLine();
System.out.println("个人的姓氏:");
surname[i + 1] = sc.nextLine();
}
}
英文:
This "flying-club" project, is to help flight-club personal to manage their work, so there are a couple of methods to serve this purpose and do so. However, I am a java-junior and not familiar quite well with the flexibility of java and its functions when it comes to a bit harder tasks like the following one: this method should check if any flight is free in a specific time frame (given by user) and print out those who actually are and if there are none, it should print out(there is unfortunately none for this time frame that can be booked). On several ways and in different methods have i taken a try but none of them work:
The method:
public static void rentFlight(int[] flightQuantity, String[] identification, String[] time, int index, boolean free) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("Which flight would you like to rent (enter the number of it)");
index = sc.nextInt();
System.out.println("For when are you renting the flight: (hh:mm)");
String z = sc.nextLine(); // time frame given
for (int i = 0; i < time.length; i++) { // iterate through the time array
if (!z.contains(String.valueOf(time))) { // if the time given doesn't match in any way, then the flight is free
System.out.println("The flight is free and can be rented: " + identification[i]);
} else {
System.out.println("There is unfortunately no free flights");
break;
}
}
}
the method where the paramteres are getting filled by input from the user:
public static void rentAll(int[] flightQuantity, String[] identification, String[] time, String[] name, String[] surname, int index, boolean free) throws ParseException {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < flightQuantity.length; i++) {
System.out.println("flight number: " + flugAnzahl[i]);
index++;
System.out.println("flight description: " + bezeichnung[i]);
System.out.println("At which time would you like to rent the flight");
System.out.println("enter the time as following: (hh:mm)");
time[i] = sc.nextLine();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm");
Date time2 = ft.parse(time[i]);
System.out.println(ft.format(time2));
Date dJetzt = new Date();
System.out.println("the time now is: " + ft.format(dJetzt));
System.out.println("the name of the person: ");
name[i + 1] = sc.nextLine();
System.out.println("the surname of the person ");
surname[i + 1] = sc.nextLine();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论