【发布时间】:2021-08-30 09:17:45
【问题描述】:
我已经编写了一个代码来将用户提供的每个约会日历存储到一个 ArrayList。每个 ArrayList 都包含标题、描述、日期和时间。
现在我想为用户提供一个选项来编辑他们选择的约会日历的数据。
我使用约会日历的标题进行比较。代码如下:
// Asks user to enter the Appointment title that they wish to edit
System.out.println("Please enter the appointment that you wish to edit: ");
String choicetitle = scan.next();
for (Appointment value : aplist)
{
// if the title of the Appointment matches with one of the data, then..
if (choicetitle.equalsIgnoreCase(value.title))
{
// ..the output occurs on the console and asks the user to enter the new data
input = new Scanner(System.in);
System.out.println("Please give the new data: \n");
System.out.println("Please give the date of the appointment! \n" +
"Year (format YYYY): ");
year = input.nextInt();
System.out.println("Month (format MM): ");
month = input.nextInt();
System.out.println("Day (format DD): ");
day = input.nextInt();
value.date = LocalDate.of(year, month, day);
System.out.println("Please give the time of the appointment! \n" +
"Hour (format hh): ");
hour = input.nextInt();
System.out.println("Minute (format mm): ");
min = input.nextInt();
value.time = LocalTime.of(hour, min);
System.out.println("Please give the title of the appointment: ");
value.title = input.next();
System.out.println("Please give the description of the appointment: ");
value.desc = input.next();
}
// if the user enters a title that doesn't exist
else
{
System.out.println("There's no appointment with this title!");
}
}
但是如果 ArrayList 中已经有 3 个元素(3 个约会日历)并且假设我要编辑第三个元素,它会给出以下输出:
There's no appointment with this title!
There's no appointment with this title!
Please give the new data:
Please give the date of the appointment!
Year (format YYYY):
有什么办法可以输出:
- 仅当用户给出的标题与 ArrayList 中的标题之一匹配时,才给出扫描器方法,
- 当用户输入的标题与 ArrayList 中的任何标题都不匹配时,只给出“此标题没有约会”?
提前致谢!
【问题讨论】:
标签: java for-loop if-statement arraylist