昨天刷完了关于异常的视频,感觉讲的太跳跃了,让我这个新手有点不适应,摸索了很久才完成了这个借书系统,擅自命名为达达借书系统,我还没习惯写备注,如果有看不懂的地方,欢迎大家留言指教。
建立 JieShuXiTong包,包内建立Book类,重写Book类构造方法,方便建立Book类的数组。
package JieShuXiTong;
public class Book {
int id;
String name;
public Book(int id,String name) {
this.id = id;
this.name = name;
// TODO Auto-generated constructor stub
}
public String getinfo(){
return id + ".t" + name;
}
}
建立XiTong类,主程序写在该类内,把借书系统拆分成三个子程序,控制子程序,查询序号子程序,查询书名子程序,在主程序中调用控制子程序,我觉得这么写便于管理。欢迎大家指教
package JieShuXiTong;
import java.util.InputMismatchException;
import java.util.Scanner;
public class XiTong {
static XiTong test = new XiTong();
static Book[] book = { new Book(1, "亮剑"), new Book(2, "血色浪漫"),
new Book(3, "JAVA学习"), new Book(4, "加勒比海盗"), new Book(5, "金刚"),
new Book(6, "钢铁侠") };
static int countid = 4;
static int countname = 4;
public XiTong() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args){
// TODO Auto-generated method stub
System.out.println("欢迎使用达达借书系统");
test.firstChoise();
}
public void firstChoise() {
System.out.println("请选择检索方法:1.图书序号 2.图书名称");
Scanner input1 = new Scanner(System.in);
int choise = input1.nextInt();
switch (choise) {
case (1):
test.findBookId();
break;
case (2):
test.findBookName();
break;
default:
System.out.println("您输入的数字信息不在可选范围之内,请重新输入");
test.firstChoise();
}
}
public void findBookId(){
Scanner input2 = new Scanner(System.in);
if (countid != 0) {
try {
System.out.println("请输入图书序号(1-6)");
int bookid = (input2.nextInt() - 1);
if (bookid }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/225/viewspace-2799264/ ,如需转载,请注明出处,否则将追究法律责任。