System.out.println("Enter the name of the user: "); names = scanner.next(); System.out.println("Enter the number of books: "); number = scanner.nextInt(); borrow = scanner.nextInt(); if (names.equals("Anna")) { number = -5; number -= borrow; }else if(names.equals("Bob")) { number = 10; number += borrow; }修改大爱吗
时间: 2023-06-13 13:08:19 浏览: 189
以下是修改后的代码:
```
System.out.println("Enter the name of the user: ");
String names = scanner.next();
System.out.println("Enter the number of books: ");
int number = scanner.nextInt();
int borrow = scanner.nextInt();
if (names.equals("Anna")) {
number -= borrow;
if (number < -5) {
System.out.println("You can't borrow more books. Return some books first.");
} else {
System.out.println("You have borrowed " + borrow + " books. You can borrow " + (5 + number) + " more books.");
}
} else if (names.equals("Bob")) {
number += borrow;
if (number > 10) {
System.out.println("You can't borrow more books. You have reached the maximum limit.");
} else {
System.out.println("You have borrowed " + borrow + " books. You can borrow " + (10 - number) + " more books.");
}
} else {
System.out.println("Invalid user. Please enter a valid user name.");
}
```
修改后的代码:
- 在Anna用户的情况下,我们将number初始化为-5,所以我们不需要再将number设置为-5。
- 如果用户尝试借更多的书籍,而它们已经达到了它们可以借的最大限制,则代码将输出适当的错误消息。
- 如果用户输入无效的用户名,则代码将输出适当的错误消息。
阅读全文