2017-02-02 14 views
-1

私はちょうどJavaで作業を始めました。私は理解できない理由でエラーが発生しました。私は見て、別の質問で私の答えを見つけることができませんでした、私はこれが繰り返しである場合は謝罪します。Xはforループ内の変数に解決できません。 Eclipse、Java

私のコードは次のとおりです。

public class Welcome { 
public static void main(String[] args) { 
    System.out.println("x\t x^2\t x^3"); 
    for(int x = 1; x < 5; x++) 
     System.out.println(x); 
     System.out.println("\t"); 
     System.out.println(x * x); 
     System.out.println("\t"); 
     System.out.println(x * x * x); 
    } 
} 

コードは、それが定義されていないxのように振る舞っているx * x * x最初の印刷のために細かい通過xを実行しますが、その後の行で、x * xとなります。このコードを実行するにはどうすればよいですか?エラーの原因は何ですか?

ありがとうございます。あなたは、中括弧を開始し、終了する場合は、ここで

にそれが唯一の最初のprintlnを実行する場合の範囲を置くのを忘れ

+1

'for(int x = 1; x <5; x ++){'と 'System.out.println(x * x * x)の後ろに中かっこを追加します。 } ' –

+0

ああ、ありがとうございます:) – Austin

答えて

1

for(int x = 1; x < 5; x++) 
{ 
    System.out.println(x); 
    System.out.println("\t"); 
    System.out.println(x * x); 
    System.out.println("\t"); 
    System.out.println(x * x * x); 
} 

を指定されていないあなたは、コード

for(int x = 1; x < 5; x++)  
    System.out.println(x);  //if you didn't specified the begin and end curly brace 
            //for range . this is the only line will work for it 
    System.out.println("\t");  //from this line, x now is undefined 
    System.out.println(x * x); //* 
    System.out.println("\t");  //* 
    System.out.println(x * x * x);//until here 
です
関連する問題