java钢铁侠3_Java入门第三季 借书系统源代码 加入了限制重输次数 欢迎指教交流...

昨天刷完了关于异常的视频,感觉讲的太跳跃了,让我这个新手有点不适应,摸索了很久才完成了这个借书系统,擅自命名为达达借书系统,我还没习惯写备注,如果有看不懂的地方,欢迎大家留言指教。

建立 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 < book.length) {

System.out.println("找到一个匹配,书名为:" + book[bookid].name);

System.out.println("感谢您的使用,再见!");

} else {

System.out.println("对不起,您搜索的序号不存在!请重新输入");

test.findBookId();

}

} catch (InputMismatchException e) {

System.out.println("您输入的信息有误,请重新输入,您还有" + (countid-1) + "次机会");

countid--;

test.findBookId();

}

} else {

System.out.println("您的输入次数已用完,感谢您的使用");

}

}

public void findBookName(){

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

Scanner input3 = new Scanner(System.in);

String bookname = input3.next();

if (countname != 0) {

try {

boolean flag = true;

for (int i = 0; i < book.length; i ++) {

if (bookname.equals(book[i].name)) {

flag = false;

System.out.println("找到一个匹配,该书序号为:" + book[i].id);

System.out.println("感谢您的使用,再见!");

break;

}

}

if(flag) {

System.out.println("对不起,您搜索的图书不存在!请重新输入");

test.findBookName();

}

} catch (InputMismatchException e) {

System.out.println("您输入的信息有误,请重新输入,您还有" + (countname-1) + "次机会");

countname--;

test.findBookName();

}

} else {

System.out.println("您的输入次数已用完,感谢您的使用");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值