新手有個問題。求個師傅帶路答疑?。?/h1>
package?com.imooc;
import?java.util.Scanner;
public?class?BorrowBook?{
String[]?shu={"高數","大學英語","計算機"};
????Scanner?sc?=?new?Scanner(System.in);
public?void?test1(){
System.out.println("輸入命令1或2:1代表按照書名查找圖書;2代表按照序號查找圖書");
}
public?void?test2()?throws?Exception{
test1();
????int?a1=sc.nextInt();
????if?(a1!=1||a1!=2){?
throw?new?Exception("命令輸入錯誤;請根據提示輸入數字命令!!");?
????}???
}
public?void?test3()?
{??try{??
test2();
}catch(Exception?e){
????test3();
}
if(a1==1){
System.out.println("請輸入圖書名");
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("請輸入圖書序號");
int?b2=?sc.nextInt();
for(int?i=0;i<3;i++){
if(b2==i)
System.out.println("book:"+shu[i]);
}
System.out.println("圖書不存在");
}
}
}
請大神看一下(main方法放到別處了),錯誤是:a1 cannot be resolved to a variable。求解釋一下哈
package?com.imooc; import?java.util.Scanner; public?class?BorrowBook?{ String[]?shu={"高數","大學英語","計算機"}; ????Scanner?sc?=?new?Scanner(System.in); public?void?test1(){ System.out.println("輸入命令1或2:1代表按照書名查找圖書;2代表按照序號查找圖書"); } public?void?test2()?throws?Exception{ test1(); ????int?a1=sc.nextInt(); ????if?(a1!=1||a1!=2){? throw?new?Exception("命令輸入錯誤;請根據提示輸入數字命令!!");? ????}??? } public?void?test3()? {??try{?? test2(); }catch(Exception?e){ ????test3(); } if(a1==1){ System.out.println("請輸入圖書名"); 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("請輸入圖書序號"); int?b2=?sc.nextInt(); for(int?i=0;i<3;i++){ if(b2==i) System.out.println("book:"+shu[i]); } System.out.println("圖書不存在"); } } }
請大神看一下(main方法放到別處了),錯誤是:a1 cannot be resolved to a variable。求解釋一下哈
2015-06-29
a1在test2里面定義過,是局部變量,在test3方法中沒有定義使用不了
2015-08-05
這代碼輸出不了啊
2015-07-06
樓上對的
2015-06-29
?= = 樓上對的。