System.out.println("请输入要修改的图书的编号:"); int book_id=scanner.nextInt();什么意思
时间: 2024-04-02 20:31:30 浏览: 66
这段代码是 Java 语言中的输入语句,作用是在控制台上输出一段提示信息 `请输入要修改的图书的编号:`,然后等待用户在控制台上输入一个整数,该整数会被保存在变量 `book_id` 中。
具体来说,`System.out.println()` 是 Java 语言中的输出语句,它会在控制台上输出一段文本,并在结尾换行。`scanner.nextInt()` 是 Java 语言中的输入语句,它会从控制台上读取一个整数,并返回给程序。这里将读取的整数赋值给 `book_id` 变量,以便后续的代码使用。
相关问题
修改代码case 4: System.out.println("Enter the name of the user: "); names = scanner.next(); System.out.println("Enter the number of books: "); number = scanner.nextInt(); if(!names.equals("Anna") && !names.equals("Bob")) { System.out.println("User "+ names +" unknown."); break;//add break to end this operation. } System.out.println("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):"); borrow = scanner.nextInt(); System.out.print("Enter the name of the user:"); names = scanner.next(); if (names.equals("Anna")) { number -= borrow; if (number < -5) { System.out.println("You can't borrow more books. Return some books first."); break; } else { System.out.println(names+" borrows " + (-5 + number -1) + "book(s)."); break; } }else if(names.equals("Bob")) { System.out.println("Enter the number of books:"); number = scanner.nextInt(); number -= borrow; if (number < 0) { System.out.println("Positive integers only!"); break; } else (number>0||number<10){ System.out.println(names+" borrows " + (10 - number +1) + " book(s)."); break; } }else { System.out.println("User "+ names +" unknown."); break; }
修改后的代码如下:
```
case 4:
System.out.println("Enter the name of the user: ");
names = scanner.next();
System.out.println("Enter the number of books: ");
number = scanner.nextInt();
if(!names.equals("Anna") && !names.equals("Bob")) {
System.out.println("User "+ names +" unknown.");
break;//add break to end this operation.
}
System.out.println("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");
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.");
break;
} else {
System.out.println(names+" borrows " + (-5 + number -1) + "book(s).");
break;
}
} else if(names.equals("Bob")) {
System.out.println("Enter the number of books:");
int newNumber = scanner.nextInt();
newNumber -= borrow;
if (newNumber < 0) {
System.out.println("Positive integers only!");
break;
} else if (newNumber > 0 || newNumber < 10){
System.out.println(names+" borrows " + (10 - newNumber +1) + " book(s).");
break;
} else {
System.out.println(names+" has already borrowed the maximum number of books.");
break;
}
} else {
System.out.println("User "+ names +" unknown.");
break;
}
```
修改的地方主要有以下几个:
1. 在第二个输入用户名的地方,变量名 `names` 被重复使用了,应该改为另一个变量名,比如 `newName`。
2. 在 Bob 用户借书的分支中,增加了一个判断,如果借书后数量等于 10,则输出已达到最大借书数量。
3. 在 Bob 用户借书的分支中,将原来的 `number` 变量改为 `newNumber`,以免与之前输入的 `number` 冲突。
4. 在 Bob 用户借书的分支中,将 `else (number>0||number<10)` 改为 `else if (newNumber > 0 || newNumber < 10)`,修正语法错误。
以上修改应该能够让程序更加健壮和正确。
修改代码public class CLI { private static Scanner input = new Scanner(System.in); public static void main(String[] args){ Library library = new Library("UIC Library"); String names; int number ; int borrow; int totalBorrowedBooks = 50; // initial value int action = 0; Scanner scanner = new Scanner(System.in); while (action != 6) { System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): "); String input = scanner.nextLine(); try { action = Integer.parseInt(input); } catch (NumberFormatException e) { System.out.println("You must type an integer!"); continue; } Object bookCounts; switch (action) { case 4: System.out.println("Enter the name of the user: "); names = scanner.next(); System.out.println("Enter the number of books: "); number = scanner.nextInt(); if(!names.equals("Anna") && !names.equals("Bob")) { System.out.println("User "+ names +" unknown."); break;//add break to end this operation. } System.out.println("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):"); borrow = scanner.nextInt(); System.out.print("Enter the name of the user:"); names = scanner.next(); 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(names+" borrows " + (-5 + number -1) + "book(s)."); } }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 if(number>0||number<10){ System.out.println(names+" borrows " + (10 - number +1) + " book(s)."); } else if(number<0){ System.out.println("Positive integers only!"); System.out.println("Enter the number of books:"); number = scanner.nextInt(); System.out.println(names+" borrows " + (10 - number +1) + " book(s)."); // Directly output the result after modification. } }else { System.out.println("User "+ names +" unknown."); } break;
public class CLI {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args){
Library library = new Library("UIC Library");
String names;
int number;
int borrow;
int totalBorrowedBooks = 50; // initial value
int action = 0;
Scanner scanner = new Scanner(System.in);
while (action != 6) {
System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): ");
String input = scanner.nextLine();
try {
action = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("You must type an integer!");
continue;
}
switch (action) {
case 4:
System.out.println("Enter the name of the user: ");
names = scanner.next();
System.out.println("Enter the number of books: ");
number = scanner.nextInt();
if(!names.equals("Anna") && !names.equals("Bob")) {
System.out.println("User "+ names +" unknown.");
break;
}
System.out.println("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");
borrow = scanner.nextInt();
System.out.print("Enter the name of the user:");
names = scanner.next();
if (names.equals("Anna")) {
number -= borrow;
if (number < -5) {
System.out.println("You can't borrow more books. Return some books first.");
break;
} else {
System.out.println(names+" borrows " + (-5 + number -1) + "book(s).");
break;
}
} 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.");
break;
} else if(number>0||number<10){
System.out.println(names+" borrows " + (10 - number +1) + " book(s).");
break;
} else if(number<0){
System.out.println("Positive integers only!");
System.out.println("Enter the number of books:");
number = scanner.nextInt();
System.out.println(names+" borrows " + (10 - number +1) + " book(s).");
break;
}
} else {
System.out.println("User "+ names +" unknown.");
break;
}
default:
break;
}
}
}
}
阅读全文