套餐 mylib
:
Library
类:
package mylib;
import java.util.*;
class Library {
public static void main(String[] args) {
boolean isInfinite = true;
int book_index;
Scanner input = new Scanner(System.in);
Book[] myBooks = new Book[3]; // Create an array of books
// Initialize each element of the array
myBooks[0] = new Book("The Lover's Dictionary", "Levithan, D.", 211);
myBooks[1] = new Book("White Tiger", "Adiga, A.", 304);
myBooks[2] = new Book("Thirteen R3asons Why", "Asher, J.", 336);
do {
// Print book listing
System.out.println("\n***** BOOK LISTING *****");
for(int i = 0; i < myBooks.length; i++) {
Book book = myBooks[i];
System.out.println("[" + (i + 1) + "] " + book.sTitle + "\nAuthor: " +
book.sAuthor + "\nPages: " + book.iPages + "\nStatus: " + book.sStatus);
System.out.print("\r\n");
}
// Select library action
System.out.println("***** SELECT ACTION *****");
System.out.println("B - Borrow a book" + "\nR - Reserve a book" +
"\nI - Return a book" + "\nX - Exit program");
System.out.print("\nEnter command: ");
String sAction = input.nextLine();
try {
switch(sAction.toUpperCase()) { // Converts input to uppercase
// Borrow a book
case "B":
System.out.println("\n***** BORROW A BOOK *****");
System.out.print("Enter book index: ");
book_index = input.nextInt();
input.nextLine();
myBooks[book_index-1].borrowBook(); // Call method from another class
break;
// Reserve a book
case "R":
System.out.println("\n***** RESERVE A BOOK *****");
System.out.print("Enter book index: ");
book_index = input.nextInt();
input.nextLine();
myBooks[book_index-1].reserveBook(); // Call method from another class
break;
// Return a book
case "I":
System.out.println("\n***** RETURN A BOOK *****");
System.out.print("Enter book index: ");
book_index = input.nextInt();
input.nextLine();
myBooks[book_index-1].returnBook(); // Call method from another class
break;
// Exit the program
case "X":
System.out.println("\nTerminating program...");
System.exit(0);
break;
default:
System.out.println("\nINVALID LIBRARY ACTION!");
break;
}
}
catch(ArrayIndexOutOfBoundsException err) {
System.out.println("\nINVALID BOOK INDEX!");
}
catch(InputMismatchException err) {
System.out.println("\nINVALID INPUT!");
}
} while(isInfinite);
}
}
Book
类:
package mylib;
class Book {
int iPages;
String sTitle, sAuthor, sStatus;
public static final String AVAILABLE = "AVAILABLE",
BORROWED = "BORROWED", RESERVED = "RESERVED";
// Constructor
public Book(String sTitle, String sAuthor, int iPages) {
this.sTitle = sTitle;
this.sAuthor = sAuthor;
this.iPages = iPages;
this.sStatus = Book.AVAILABLE; // Initializes book status to AVAILABLE
}
// Constructor accepts no arguments
public Book() {
}
// Borrow book method
void borrowBook() {
if(sStatus.equals(Book.AVAILABLE) || sStatus.equals(Book.RESERVED)) {
sStatus = Book.BORROWED;
System.out.println("\nBORROW SUCCESSFUL!");
}
else {
System.out.println("\nBOOK IS UNAVAILABLE!");
}
}
// Reserve book method
void reserveBook() {
if(sStatus.equals(Book.AVAILABLE)) {
sStatus = Book.RESERVED;
System.out.println("\nRESERVE SUCCESSFUL!");
}
else {
System.out.println("\nBOOK IS UNAVAILABLE!");
}
}
// Return book method
void returnBook() {
if(sStatus.equals(Book.AVAILABLE)) {
System.out.println("\nBOOK IS ALREADY AVAILABLE!");
}
else if(sStatus.equals(Book.RESERVED)) {
System.out.println("\nBOOK IS ALREADY RESERVED!");
}
else {
sStatus = Book.AVAILABLE;
}
}
}
当我输入无效的图书索引时,例如4,错误被捕获并打印出“无效的书籍索引!”
但是,当我为图书索引输入字符或字符串时,会打印“无效的图书馆操作!”何时应该打印“INVALID INPUT!”
默认子句似乎会覆盖catch?
答案 0 :(得分:1)
您的sAction
变量始终为String
,因为Scanner.nextLine()
会返回String
。
因此,您的default
语句被触发,并且假设InputMismatchException
catch永远不会执行是合理的。
如果您想微调输入接受度,请参阅其他Scanner“下一步”方法。
示例:
while (true) { // your infinite loop - better use a while-do instead of a do-while here
String sAction = input.nextLine(); // we assign sAction iteratively until user "quits"
// No try-catch: ArrayIndexOutOfBoundsException is unchecked and you shouldn't catch it.
// If it bugs, fix the code.
// No InputMismatchException either, as you don't need it if you use nextLine
// your switch same as before
}
答案 1 :(得分:0)
由于try-catch适用于int book_chosen
而不 String sAction
,因此当我输入一个时,删除任何catch语句抛出OutOfBounds或InputMismatch错误int大于3(OutOfBounds)或int book_chosen
(Enter book index:
)的char / string(InputMismatch)。
我发现当两个catch语句都存在但没有显示相应的消息时,它会捕获错误。由于所有这些都是“无效”输入,我只是将所有错误提示更改为INVALID INPUT!
Library
课程:
package mylib;
import java.util.*;
class Library {
static int book_index;
static Scanner input = new Scanner(System.in);
static void getIndex() {
System.out.print("Enter book index: ");
book_index = input.nextInt();
input.nextLine();
}
public static void main(String[] args) {
// Create an array of books
Book[] myBooks = new Book[3];
// Initialize each element of the array
myBooks[0] = new Book("The Lover's Dictionary", "Levithan, D.", 211);
myBooks[1] = new Book("White Tiger", "Adiga, A.", 304);
myBooks[2] = new Book("Thirteen R3asons Why", "Asher, J.", 336);
while(true) {
// Print book listing
System.out.println("\n***** BOOK LISTING *****");
for(int i = 0; i < myBooks.length; i++) {
Book book = myBooks[i];
System.out.println("[" + (i + 1) + "] " + book.sTitle +
"\nAuthor: " + book.sAuthor + "\nPages: " +
book.iPages + "\nStatus: " + book.sStatus);
System.out.print("\r\n");
}
// Select library action
System.out.println("***** SELECT ACTION *****");
System.out.println("B - Borrow a book" + "\nR - Reserve a book" +
"\nI - Return a book" + "\nX - Exit program");
System.out.print("\nEnter command: ");
String sAction = input.nextLine();
try {
switch(sAction.toUpperCase()) {
// Borrow a book
case "B":
System.out.println("\n***** BORROW A BOOK *****");
getIndex();
myBooks[book_index-1].borrowBook();
break;
// Reserve a book
case "R":
System.out.println("\n***** RESERVE A BOOK *****");
getIndex();
myBooks[book_index-1].reserveBook();
break;
// Return a book
case "I":
System.out.println("\n***** RETURN A BOOK *****");
getIndex();
myBooks[book_index-1].returnBook();
break;
// Exit the program
case "X":
System.out.println("\nTerminating program...");
System.exit(0);
break;
default:
System.out.println("\nINVALID INPUT!");
break;
}
}
catch(ArrayIndexOutOfBoundsException err) {
System.out.println("\nINVALID INPUT!");
}
catch(InputMismatchException err) {
System.out.println("\nINVALID INPUT!");
}
}
}
}
Book
课程:
package mylib;
class Book {
int iPages;
String sTitle, sAuthor, sStatus;
public static final String AVAILABLE = "AVAILABLE",
BORROWED = "BORROWED", RESERVED = "RESERVED";
// Constructor
public Book(String sTitle, String sAuthor, int iPages) {
this.sTitle = sTitle;
this.sAuthor = sAuthor;
this.iPages = iPages;
this.sStatus = Book.AVAILABLE;
}
// Constructor accepts no arguments
public Book() {
}
// Borrow book method
void borrowBook() {
if(sStatus.equals(Book.AVAILABLE) || sStatus.equals(Book.RESERVED)) {
sStatus = Book.BORROWED;
System.out.println("\nBORROW SUCCESSFUL!");
}
else {
System.out.println("\nBOOK IS UNAVAILABLE!");
}
}
// Reserve book method
void reserveBook() {
if(sStatus.equals(Book.AVAILABLE)) {
sStatus = Book.RESERVED;
System.out.println("\nRESERVE SUCCESSFUL!");
}
else {
System.out.println("\nBOOK IS UNAVAILABLE!");
}
}
// Return book method
void returnBook() {
if(sStatus.equals(Book.AVAILABLE)) {
System.out.println("\nBOOK IS AVAILABLE!");
}
else if(sStatus.equals(Book.RESERVED)) {
System.out.println("\nBOOK IS RESERVED!");
}
else {
sStatus = Book.AVAILABLE;
System.out.println("\nRETURN SUCCESSFUL!");
}
}
}