package?library1;
public?class?Book?{
int?num;
String?name;
public?Book(int?num,String?name){
this.num=num;
this.name=name;
}
?Book[]books={new?Book(1,"數(shù)據(jù)結(jié)構(gòu)"),
new?Book(2,"線性數(shù)學(xué)"),new?Book(3,"高等數(shù)學(xué)")};
}
package?library1;
import?java.util.Scanner;
public?class?Library?{
public?void?library(){
System.out.println("輸入命令:1.按照名稱查找圖書"
+"2.按照序號(hào)查找圖書");
try{
Scanner?sc=new?Scanner(System.in);
int?a=sc.nextInt();
if(a==1){
library1();
}else?if(a==2){
library2();
}else{
System.out.println("您輸入的數(shù)據(jù)有誤,請(qǐng)重新輸入(1或2)");
}
}catch(Exception?e){
System.out.println("命令輸入有誤,請(qǐng)?zhí)崾据斎朊?);
library();//重新調(diào)用方法,實(shí)現(xiàn)數(shù)據(jù)回滾
}
}
//按照?qǐng)D書名稱查找
public?void?library1(){
System.out.println("請(qǐng)輸入圖書名稱:");
try{
Scanner?sc=new?Scanner(System.in);
String?b=sc.next();
for(Book?book:books){
if(book.name.equals(b)){
System.out.println("books:"+book.name);
break;
}
}
}catch(Exception?e){
System.out.println("圖書不存在!");
library1();
}
}
//按照?qǐng)D書序號(hào)查找
public?void?library2(){
System.out.println("請(qǐng)輸入圖書名稱:");
try{
Scanner?sc=new?Scanner(System.in);
int?c=sc.nextInt();
for(Book?book:books){
if(book.num==c){
System.out.println("books:"+book.name);
break;
}
}
}catch(Exception?e){
System.out.println("圖書不存在!");
library2();
}
}
}
package?library1;
public?class?Initail?{
public?static?void?main(String[]?args)?{
//?TODO?自動(dòng)生成的方法存根
System.out.println("歡迎登陸借書系統(tǒng)");
Library?lb=new?Library();
lb.library();
}
}
2016-05-12
foreach操作數(shù)組語(yǔ)法:
foreach(元素類型 ?元素變量: 遍歷對(duì)象){
執(zhí)行代碼 ? ? ? ? ?
}
我覺得是是你沒有搞清楚這個(gè)概念問題,另外我感覺你的程序?qū)懙牟诲e(cuò)!贊
2016-04-10
神馬?請(qǐng)教詳解
2016-04-10
與變量的作用域有關(guān)?