新手有個(gè)問(wèn)題。求個(gè)師傅帶路答疑?。?/h1>
package?com.imooc;
import?java.util.Scanner;
public?class?BorrowBook?{
String[]?shu={"高數(shù)","大學(xué)英語(yǔ)","計(jì)算機(jī)"};
????Scanner?sc?=?new?Scanner(System.in);
public?void?test1(){
System.out.println("輸入命令1或2:1代表按照書名查找圖書;2代表按照序號(hào)查找圖書");
}
public?void?test2()?throws?Exception{
test1();
????int?a1=sc.nextInt();
????if?(a1!=1||a1!=2){?
throw?new?Exception("命令輸入錯(cuò)誤;請(qǐng)根據(jù)提示輸入數(shù)字命令!!");?
????}???
}
public?void?test3()?
{??try{??
test2();
}catch(Exception?e){
????test3();
}
if(a1==1){
System.out.println("請(qǐng)輸入圖書名");
String?a2=sc.next();
for(int?i=0;i<3;i++){
if(a2==shu[i])
System.out.println("book:"+a2);
}
}
else?if(a1==2)
{
System.out.println("請(qǐng)輸入圖書序號(hào)");
int?b2=?sc.nextInt();
for(int?i=0;i<3;i++){
if(b2==i)
System.out.println("book:"+shu[i]);
}
System.out.println("圖書不存在");
}
}
}
請(qǐng)大神看一下(main方法放到別處了),錯(cuò)誤是:a1 cannot be resolved to a variable。求解釋一下哈
package?com.imooc; import?java.util.Scanner; public?class?BorrowBook?{ String[]?shu={"高數(shù)","大學(xué)英語(yǔ)","計(jì)算機(jī)"}; ????Scanner?sc?=?new?Scanner(System.in); public?void?test1(){ System.out.println("輸入命令1或2:1代表按照書名查找圖書;2代表按照序號(hào)查找圖書"); } public?void?test2()?throws?Exception{ test1(); ????int?a1=sc.nextInt(); ????if?(a1!=1||a1!=2){? throw?new?Exception("命令輸入錯(cuò)誤;請(qǐng)根據(jù)提示輸入數(shù)字命令!!");? ????}??? } public?void?test3()? {??try{?? test2(); }catch(Exception?e){ ????test3(); } if(a1==1){ System.out.println("請(qǐng)輸入圖書名"); String?a2=sc.next(); for(int?i=0;i<3;i++){ if(a2==shu[i]) System.out.println("book:"+a2); } } else?if(a1==2) { System.out.println("請(qǐng)輸入圖書序號(hào)"); int?b2=?sc.nextInt(); for(int?i=0;i<3;i++){ if(b2==i) System.out.println("book:"+shu[i]); } System.out.println("圖書不存在"); } } }
請(qǐng)大神看一下(main方法放到別處了),錯(cuò)誤是:a1 cannot be resolved to a variable。求解釋一下哈
2015-06-29
a1在test2里面定義過(guò),是局部變量,在test3方法中沒(méi)有定義使用不了