2012-04-18 84 views
0

這是什麼問題;int爲字節,「無法解析爲變量」。哪裏不對?

public class cast { 

    public static void main(String args[]){ 
     double x, y; 
     int i; 
     char c; 

     x = 10.0; 
     y = 3.0; 

     i = (int) (x/y); 
     System.out.println("Integer outcome of x/y = " + i); 

     i = 100; //Assaigning new value to i. 

     b = (byte) i; 
     System.out.println("The value of i is: " + b); 

    } 

} 

它給了我下面的錯誤信息; b不能解析爲變量。

雖然我沒有,正是因爲他們在書中做了(我認爲,重新讀了這本書指令像五次....)

回答

5

b是不是在你的例子聲明的變量。在你使用變量之前,你必須聲明它。如果你不這樣做Java不知道什麼b代表,並不知道如何處理它。嘗試

byte b = (byte) i; 

代替。或者,您也可以添加行

byte b; 

到您的程序的開始。這告訴Java你想要b是一個可以容納一個字節的變量。