为了账号安全,请及时绑定邮箱和手机立即绑定

我也发一发

package bookborrowingsystem;


public class Book {

private int num;

private String name;

public Book(int num,String name) {

this.num = num;

this.name = name;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

=============================================================================

package bookborrowingsystem;

import java.util.Scanner;


public class BookBorrowingSystem {

private Book[] books = {new Book(1,"Java入门第一季教程"),new Book(2,"Java入门第二季教程"),new Book(3,"Java入门第三季教程")};

private static Scanner input = new Scanner(System.in);

public String findBookNum() {

while(true) {

System.out.println("输入图书序号");

if(!input.hasNextInt()){

System.out.println("输入不匹配异常!请根据提示输入数字命令!");

input.nextLine();

continue;

}

int num = input.nextInt();

for(Book book:books) {

if(book.getNum()==num) {

return book.getName();

}

System.out.println("图书不存在!");

break;

}

}

}

public String findBookName() throws Exception {

while(true) {

System.out.println("输入图书名称");

String name = input.next();

for(Book book:books) {

if(book.getName().equals(name)) {

return book.getName();

}

System.out.println("图书不存在!");

break;

}

}

}

}

=============================================================================

package bookborrowingsystem;

import java.util.InputMismatchException;

import java.util.Scanner;


public class Test {

public static void main(String[] args) {

BookBorrowingSystem bookBorrowingSystem = new BookBorrowingSystem();

Scanner input = new Scanner(System.in);

while(true){

try {

System.out.println("模拟借书系统:1-按照序号查找图书;2-按照名称查找图书");

int choice = input.nextInt();//

String bookName = null;

if(choice==1) {

bookName = bookBorrowingSystem.findBookNum();

}else if(choice==2) {

bookName = bookBorrowingSystem.findBookName();

}else {

System.out.println("命令输入错误!请根据提示输入数字命令!");

continue;

}

if(bookName == null) {

System.out.println("图书不存在!");

continue;

}

System.out.println("找到这本名为《"+bookName+"》的书");

break;

}catch (InputMismatchException e) {

System.out.println("输入不匹配异常!请根据提示输入数字命令!");

input.nextLine();

}catch(Exception e) {

System.out.println(e.getMessage());

}

}

}

}

正在回答

1 回答

小伙子,不错哒

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409789    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

我也发一发

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信