珂珂凉
2016-04-15 22:14
老师布置胡作业谁做了,借我看看哦
package library;
import java.util.Scanner;
public class BookScanner {
public static void main(String[] args) {
String []bookName={"毛概","光纤通信","星系奇缘","量子力学","goutou大美","都市妖侠传","七剑下天山"};
while(true){
System.out.println("欢迎来到工大图书馆查询系统");
System.out.println("请输入命令:1-按照名称查找图书;2-按照序号查询图书");
Scanner input=new Scanner(System.in);
String book;
try{
int a=input.nextInt();
switch(a){
case 1:
System.out.println("请输入你要查找的图书名");
book=bookname(bookName);
System.out.println("book:"+"《"+book+"》");
break;
case 2:
System.out.println("请输入你所查找图书序号");
book=booknum(bookName);
System.out.println("book:"+"《"+book+"》");
break;
default://其他值的命令均认为是错误命令
System.out.println("命令输入错误!");
continue;
}
break;
}catch(Exception e){
}
}
}
public static String bookname(String[] bookName)throws Exception {
System.out.println("输入图书名称:");
Scanner in=new Scanner(System.in);
//获取输入的图书名称
String name = in.next();
for (int i = 0; i < bookName.length; i++) {
if (name.equals(bookName[i]))
//输入的名称与某一图书名称匹配,返回该图书
return bookName[i];
}
//若无匹配,抛出”图书不存在异常“
throw new Exception("图书不存在!");
}
public static String booknum(String[] bookName)throws Exception {
System.out.println("输入图书序号:");
Scanner in=new Scanner(System.in);
//获取输入的图书名称
int b = in.nextInt();
for (int i = 0; i < bookName.length; i++) {
if (b==i)
//输入的名称与某一图书名称匹配,返回该图书
return bookName[i];
}
//若无匹配,抛出”图书不存在异常“
throw new Exception("图书不存在!");
}
}
public static void Bookstart()throws Exception{
System.out.println("******书号查询号请按:1;书名查询请按:0******");
Scanner sc = new Scanner(System.in);
try {
int ture = sc.nextInt();
if(ture==1){
System.out.println("******输入图书"+"序号******");
BookNum();
}
else if(ture==0){
System.out.println("******输入图书"+"名称******");
BookName();
}
else{
throw new Exception("******输入信息有误请重新输入(1/0)******");
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入(1/0)******");
Bookstart();
}
}
这段输入非数字不会死循环么?比如说输入“ssss”
package test5;
import java.util.Scanner;
public class Booksystem {
static String[] bookname={"高数","程序编程","大学英语","操作系统","Java"};
public static void main(String[] args) throws Exception {
Bookstart();
}
public static void Bookstart()throws Exception{
System.out.println("******书号查询号请按:1;书名查询请按:0******");
Scanner sc = new Scanner(System.in);
try {
int ture = sc.nextInt();
if(ture==1){
System.out.println("******输入图书"+"序号******");
BookNum();
}
else if(ture==0){
System.out.println("******输入图书"+"名称******");
BookName();
}
else{
throw new Exception("******输入信息有误请重新输入(1/0)******");
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入(1/0)******");
Bookstart();
}
}
public static void BookNum()throws Exception{
Scanner sc = new Scanner(System.in);
try {
int num = sc.nextInt();
if(num<=bookname.length&&num>0){
System.out.println("******序号"+num+"."+bookname[num-1]+"******");
}
else{
throw new Exception();
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入******");
BookNum();
}
Bookstart();
}
public static void BookName() throws Exception{
Scanner sc = new Scanner(System.in);
try {
String name = sc.next();
for(int i = 0;i<bookname.length;i++){
if(bookname[i].equals(name)){
int num=i+1;
System.out.println("******序号"+num+"."+bookname[i]+"******");
break;
}else{
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入******");
BookName();
}
Bookstart();
}
}
package tushu;
import java.util.Scanner;
public class book {
int bookNum;
String bookName;
public book(int bookNum,String bookName){
this.bookNum=bookNum;
this.bookName=bookName;
}
public static void choose1(String bookName,book[] book){
int k=0;
for(int i=0;i<book.length;i++){
if(book[i].bookName.equals(bookName)){
System.out.println("book:"+book[i].bookName);
k=1;
}
}
if(k!=1){
System.out.println("图书不存在!");
}
}
public static void choose2(int bookNum,book[] book){
int k=0;
for(int i=0;i<book.length;i++){
if(book[i].bookNum==bookNum){
System.out.println("book:"+book[i].bookName);
k=1;
}
}
if(k!=1){
System.out.println("图书不存在!");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
book[] book=new book[10];
book book0=new book(1,"高数");
book book1=new book(2,"数据结构");
book book2=new book(3,"体育");
book book3=new book(4,"离散数学");
book book4=new book(5,"微机");
book book5=new book(6,"PHP");
book book6=new book(7,"MySql");
book book7=new book(8,"英语");
book book8=new book(9,"计算机网络");
book book9=new book(10,"C语言");
book[0]=book0;
book[1]=book1;
book[2]=book2;
book[3]=book3;
book[4]=book4;
book[5]=book5;
book[6]=book6;
book[7]=book7;
book[8]=book8;
book[9]=book9;
while(true){
System.out.println("输入命令:1--按照名称查找图书 2--按照序号查找图书 ");
Scanner i=new Scanner(System.in);
try {
int choose = i.nextInt();
if (choose == 1) {
System.out.println("请输入要查找图书的书名:");
Scanner t=new Scanner(System.in);
String bookName=t.next();
choose1(bookName,book);
} else if (choose == 2) {
System.out.println("请输入要查找图书的编号:");
Scanner t=new Scanner(System.in);
int bookNum=t.nextInt();
choose2(bookNum,book);
} else {
//重新初始化系统
main(null);
}
} catch (Exception e) {
System.out.println("输入类型有误");
}
}
}
}
public class Booksystem {
static Scanner sc = new Scanner(System.in);
static String[] bookname={"高数","程序编程","大学英语","操作系统","Java"};
public static void main(String[] args) throws Exception {
System.out.println("******书号查询号请按:1;书名查询请按:0:******");
Bookstart();
}
public static void Bookstart()throws Exception{
try {
int ture = sc.nextInt();
if(ture==1){
System.out.println("******输入图书"+"序号******");
BookNum();
}
else if(ture==0){
System.out.println("******输入图书"+"名称******");
BookName();
}
else{
throw new Exception("******输入信息有误请重新输入(1/0)******");
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入(1/0)******");
Bookstart();
}
}
public static void BookNum()throws Exception{
try {
int num = sc.nextInt();
if(num<=bookname.length&&num>0){
System.out.println("******序号"+num+"."+bookname[num-1]+"******");
}
else{
throw new Exception("******输入信息有误请重新输入******");
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入******");
BookNum();
}
}
public static void BookName() throws Exception{
try {
String name = sc.next();
for(int i = 0;i<bookname.length;i++){
if(name == bookname[i]){
System.out.println("******序号"+i+1+"."+name+"******");
}else{
throw new Exception("******输入信息有误请重新输入******");
}
}
} catch (Exception e) {
System.out.println("******输入信息有误请重新输入******");
BookName();
}
}
}
Java入门第三季
409792 学习 · 4340 问题
相似问题