我正在尝试使用java创建一个模式,如下图所示。有人可以请正确告诉我我的代码有什么问题吗?
package welcome;
import java.util.Scanner;
public class Welcome
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("enter the rows");
int rows= sc.nextInt();
System.out.println("here is the pattern");
int i, j, k=1;
for(i=1; i<=rows; i++)
{
for(j=1; j<=k; j++)
{
System.out.print("* " +j);
}
k = k + 2;
System.out.println();
}
}
}
结果是这样的 这是模式
* 1
* 1* 2* 3
* 1* 2* 3* 4* 5
* 1* 2* 3* 4* 5* 6* 7
* 1* 2* 3* 4* 5* 6* 7* 8* 9
答案 0 :(得分:-1)
试试这个:
package welcome;
import java.util.Scanner;
public class Welcome
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("enter the rows");
int rows= sc.nextInt();
System.out.println("here is the pattern");
int i, j, k=1;
String tmp="";
for(i=1; i<=rows; i++)
{
for(j=1; j<=k; j++)
{
tmp = tmp + "*";
}
k = k + 2;
System.out.println(tmp+ j +"\n");
tmp = "";
}
}
}