0

我一直在尝试打印出一个 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]

4

3 回答 3

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
于 2013-04-20T05:57:08.593 回答
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("*");

 }
}
于 2013-04-20T04:55:16.350 回答
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]);
}
   }
  }
 }
于 2013-04-20T04:59:44.577 回答