我一直在尝试打印出一个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 :(得分: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("*");
}
}
答案 1 :(得分: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]);
}
}
}
}
答案 2 :(得分: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