开始Java:使用数组打印出一个5点星
我一直试图打印出一个5点星,但我不断得到编译错误开始Java:使用数组打印出一个5点星,java,compiler-errors,Java,Compiler Errors,我一直试图打印出一个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++)
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]);
}
}
}
}
我不明白为什么我们不能说(char)star1[I][j]=“*”
否则我怎么能给star1[I][j]
分配星号 错误就在这里
改变
int[][] star1 =new int[first][last];
int first = 5;
int last = 2;
到
并将(int j=0;j<2;j++)的更改为(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("*");
}
}
publicstaticvoidmain(字符串[]args){
int first=5;
int last=2;
int[]star1=新int[第一个][最后一个];
对于(int i=0;i<5;i++)
对于(int j=0;j<2;j++)
System.out.println(“*”);
}
}
我修复了编译器的错误:
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]);
}
}
}
}
公共级明星{
公共静态void main(字符串[]args){
int first=5;
int last=2;
char[][]star1=新字符[第一个][最后一个];
对于(int i=0;i<5;i++)
{
对于(int j=0;j<2;j++){
star1[i][j]='*';
System.out.println(star1[i][j]);
}
}
}
}
公共级明星{
公共静态void main(字符串[]args){
对于(int i=0;i<5;i++){
对于(int j=0;j<2;j++){
系统输出打印(“*”);
}
}
}
}
O/P:**********
以上程序是一个简单的程序
但是在你的节目里
你的声明是
int[]star1=新int[第一个][最后一个];
int first=5;
int last=2;
在使用变量之前,应该声明变量
int first=5;
int last=2;
int[]star1=新int[第一个][最后一个];
然后
对于(int i=0;i<5;i++){
对于(int j=0;j<2;j++){
star1[i][j]='*';
系统输出打印(star1[i][j]);
}
}
}
}
上面的代码将成功编译,但o/p如下
42424242424242424242
“获取编译错误”。将错误复制/粘贴为一个文件。对代码块也使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程!
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("*");
}
}
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]);
}
}
}
}
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