求解答??!大神
import java.util.Scanner;
public class Exe {
public static void main(String[] args) {
// TODO Auto-generated method stub
Exe.show();
}
public static String[] books={"計(jì)算機(jī)科學(xué)","C語言程序設(shè)計(jì)","數(shù)據(jù)結(jié)構(gòu)","微積分"};
public static void show(){
Scanner input=new Scanner(System.in);
System.out.println("1-按照名稱查找圖書,2-按照序號查早圖書");
try{
int num=input.nextInt();
switch(num){
case 1:
System.out.println("請輸入圖書名稱");
? ?String name=input.next();
? ?for(int i=0;i<books.length;i++){
? ? if(name.equals(books[i])){ ? ? ? ? ? ?//為什么這里我寫成"name==books[i]運(yùn)行結(jié)果會是不存在呢
? ? System.out.println("books:"+books[i]);
? ? return;
? ? }
? ?
? ?
? ?
? ?
? ?}
? ?System.out.println("圖書不存在,請重新輸入");
? ? show();
? ?
case 2:
System.out.println("請輸入圖書序號");
int n=input.nextInt();
for(int i=0;i<books.length;i++){
if(n==i){
System.out.println("books:"+books[n]);
return;
}
}
System.out.println("圖書不存在,請重新輸入序列號");
show();
}
}
catch(Exception e){
System.out.println("命令輸入有誤,請重新輸入");
show();
}
}
}
2015-08-02
字符串的equals()方法是判斷內(nèi)容是否一樣,而用==的話就是判斷引用是否一致。