2013-04-20 75 views
0

我一直在試圖打印出一個5尖的星,但我一直在收到編譯錯誤。開始Java:使用數組打印出一個5尖的星

public class star 
{ 


    public static void main(String[] args) 
    { 

    int[][] star1 =new int[first][last]; 
    int first = 5; 
    int last = 2; 

    for(int i = 0; i < 5; i++) 
    { 
     for(int j = 0; j < 2; j++) 

     (char) star1[i][j] == "*"; 

     System.out.println(star1[i][j]); 
    } 
    } 
} 
} 

這是我得到的錯誤:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    first cannot be resolved to a variable 
    last cannot be resolved to a variable 
    Syntax error on token ")", throw expected after this token 
    Incompatible operand types char and String 
    j cannot be resolved to a variable at star.main(star.java:7) 

我不明白爲什麼我們不能說(char) star1[i][j] == "*"我還能如何分配一個星號star1[i][j]

+0

* 「收到編譯錯誤。」 *複製/粘貼錯誤爲[編輯的問題](http://stackoverflow.com/posts/16116714/edit)。還爲代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程! – 2013-04-20 04:55:16

回答

0

錯誤是在這裏

變化

int[][] star1 =new int[first][last]; 
int first = 5; 
int last = 2; 

int first = 5; 
int last = 2; 

int[][] star1 =new int[first][last]; 

,改變for(int j = 0; j < 2; j++)for(int j = 0; j < 2; j++){ //你錯過{

使用需要聲明它們的變量之前。

公共類星{

public static void main(String[] args){ 

    int first = 5; 
    int last = 2; 
     int[][] star1 =new int[first][last]; 


     for(int i = 0; i < 5; i++)   
      for(int j = 0; j < 2; j++) 
      System.out.println("*"); 

} 
} 
0

我固定的編譯器錯誤:

public class star { 


public static void main(String[] args){ 
    int first = 5; 
    int last = 2; 
char[][] star1 =new char[first][last]; 


for(int i = 0; i < 5; i++) 
{ 
for(int j = 0; j < 2; j++){ 

star1[i][j] = '*'; 

System.out.println(star1[i][j]); 
} 
    } 
    } 
} 
0
public class star { 

    public static void main(String[] args) { 
     for (int i = 0; i < 5; i++) { 
      for (int j = 0; j < 2; j++) { 
       System.out.print("*"); 
      } 
     } 
    } 
} 

O/P:********** 

Above program is a simple program 
But in your program 
Your declaration is 
int[][] star1 =new int[first][last]; 
    int first = 5; 
    int last = 2; 
You should declare the variables before you use that variables 
int first = 5; 
int last = 2; 
    int[][] star1 =new int[first][last]; 

And then 
for(int i = 0; i < 5; i++){ 

     for(int j = 0; j < 2; j++){ 


     star1[i][j] = '*'; 

     System.out.print(star1[i][j]); 
    } 
    } 
    } 
} 

The above code will compile successfully but the o/p is like 
42424242424242424242