System.out.print("Enter your guess(0to quit):"); guess=scan.nextInt(); scan.nextLine();是什么意思
时间: 2024-06-07 20:08:02 浏览: 7
这段代码是Java语言中的输入语句,用于读取用户在控制台中输入的信息。具体来说:
- `System.out.print("Enter your guess(0 to quit):");` 会在控制台中输出提示信息,告诉用户需要输入猜测的数字,以及在需要时可以输入0来退出程序。
- `guess=scan.nextInt();` 会读取用户输入的一个整数,并将其赋值给变量 `guess`。
- `scan.nextLine();` 用于读取输入行末的换行符,以便下一次输入可以从新的一行开始。
需要注意的是,如果用户输入的内容不是整数,则会抛出异常。因此在实际使用中,需要对输入进行合法性检查和异常处理。
相关问题
帮我修改一下我的代码错误:帮我看看我这段代码有什么错误:int choice = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): "); // Perform the selected action while (choice != 6) { switch (choice) { case 1: System.out.println("Total number of borrowed books: " + Library.totalBorrowedBooks(null)); break; case 2: System.out.print("Type the user role (lender:1 borrower:2): "); int role = readPosInt(null); if (role == 1) { System.out.print("Enter the name of the user: "); String name = readLine(name); System.out.print("Enter the initial number of lent books: "); int numBooks = readPosInt(null); Lender lender = new Lender(name, numBooks); Library.addUser(lender); System.out.println("Lender "" + name + "" lending " + numBooks + " book(s) has been added."); } else if (role == 2) { System.out.print("Enter the name of the user: "); String name = readLine(name); System.out.print("Enter the initial number of borrowed books: "); int numBooks = readPosInt(null); Borrower borrower = new Borrower(name, numBooks); Library.addUser(borrower); System.out.println("Borrower "" + name + "" borrowing " + numBooks + " book(s) has been added."); } else { System.out.println("Unknown user role!"); } break; case 3: System.out.print("Enter the name of the user: "); String username = input.nextLine(); try { int numBorrowed = Library.totalBorrowedBooks(username); System.out.println(username + " borrows " + numBorrowed + " book(s)."); } catch (UnknownUserException e) { System.out.println("User " + username + " unknown."); } break; case 4: try { System.out.print("Enter the name of the user: "); String name = input.nextLine(); System.out.print("Enter the number of books: "); int num = input.nextInt(); input.nextLine(); library.moreBook(username, role); } catch (UnknownUserException e) { System.out.println("User " + username + " unknown."); } break; case 5: System.out.print("Enter the name of the user: "); username = input.next(); System.out.print("Enter the number of books: "); int numBooks = input.nextInt(); library.moreBook(username, -numBooks); // simulate decreasing books break; case 6: System.out.println("Goodbye!"); System.exit(0); break; }
在这段代码中,有几个错误需要修改:
1. 在第6行,readPosInt方法的参数为"Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): ",应该改为null。
2. 在第17行,Lender和Borrower的构造函数需要传入参数name和numBooks,应该改为lender和borrower。
3. 在第31行,input.nextLine()应该放在int role = readPosInt(null);的后面。
4. 在第37行,library应该改为Library。
5. 在第44行,input.next()应该改为input.nextLine()。
修改后的代码如下:
```
int choice = readPosInt(null);
// Perform the selected action
while (choice != 6) {
switch (choice) {
case 1:
System.out.println("Total number of borrowed books: " + Library.totalBorrowedBooks(null));
break;
case 2:
System.out.print("Type the user role (lender:1 borrower:2): ");
int role = readPosInt(null);
if (role == 1) {
System.out.print("Enter the name of the user: ");
String name = readLine(null);
System.out.print("Enter the initial number of lent books: ");
int numBooks = readPosInt(null);
Lender lender = new Lender(name, numBooks);
Library.addUser(lender);
System.out.println("Lender " + name + " lending " + numBooks + " book(s) has been added.");
} else if (role == 2) {
System.out.print("Enter the name of the user: ");
String name = readLine(null);
System.out.print("Enter the initial number of borrowed books: ");
int numBooks = readPosInt(null);
Borrower borrower = new Borrower(name, numBooks);
Library.addUser(borrower);
System.out.println("Borrower " + name + " borrowing " + numBooks + " book(s) has been added.");
} else {
System.out.println("Unknown user role!");
}
break;
case 3:
System.out.print("Enter the name of the user: ");
String username = input.nextLine();
try {
int numBorrowed = Library.totalBorrowedBooks(username);
System.out.println(username + " borrows " + numBorrowed + " book(s).");
} catch (UnknownUserException e) {
System.out.println("User " + username + " unknown.");
}
break;
case 4:
try {
System.out.print("Enter the name of the user: ");
String name = input.nextLine();
System.out.print("Enter the number of books: ");
int num = input.nextInt();
input.nextLine();
Library.moreBook(name, num);
} catch (UnknownUserException e) {
System.out.println("User unknown.");
}
break;
case 5:
System.out.print("Enter the name of the user: ");
username = input.nextLine();
System.out.print("Enter the number of books: ");
int numBooks = input.nextInt();
input.nextLine();
Library.moreBook(username, -numBooks); // simulate decreasing books
break;
case 6:
System.out.println("Goodbye!");
System.exit(0);
break;
}
choice = readPosInt(null);
}
```
修改代码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;
}
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)