java图书凭租_如何通过java一步实现租书系统
package first;
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
String[] books = new String[5];
books[0] = "高數";
books[1] = "線代";
books[2] = "英語";
books[3] = "化學";
books[4] = "歷史";
Hello h = new Hello();
int num = h.welcome();
while(true) {
if (num == 1) {
try {
if (h.byName(books)){
break;
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
System.out.println("圖書不存在!");
num = h.welcome();
}
} else if (num == 2) {
try {
if (h.byNum(books)) {
break;
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
System.out.println("圖書不存在!");
num = h.welcome();
}
} else {
System.out.println("NUM:"+num);
break;
}
}
}
public int welcome(){
System.out.println("輸入命令: 1-按照名稱查找圖書 2-按照序號查找圖書");
Scanner sin = new Scanner(System.in);
int num = 0;
try {
num = sin.nextInt();
} catch (Exception e) {
// TODO: handle exception
System.out.println("命令錯誤!請根據提示輸入數字命令!");
num = welcome();
}
return num;
}
public boolean byName(String[] books) throws NotFoundException{
Scanner sin = new Scanner(System.in);
System.out.println("輸入圖書名稱:");
String name = sin.next();
for (int i = 0; i < books.length;i++) {
if (books[i].equals(name) == true) {
System.out.println("book:"+name);
return true;
}
}
throw new NotFoundException("圖書不存在!");
}
public boolean byNum(String[] books) throws NotFoundException{
Scanner sin = new Scanner(System.in);
System.out.println("輸入圖書序號:");
int num;
try {
num = sin.nextInt();
} catch (Exception e) {
// TODO: handle exception
throw new NotFoundException("圖書不存在!");
}
if (num < 0 || num > books.length) {
throw new NotFoundException("圖書不存在!");
}
System.out.println("book:"+books[num]);
return true;
}
}
總結
以上是生活随笔為你收集整理的java图书凭租_如何通过java一步实现租书系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中上转型概念_在java 中,什
- 下一篇: java z注释过滤_如何编写一个jav