[英]Number Pattern in Java
I am trying to make a program that outputs 我正在尝试制作一个输出的程序
So far I have done: 到目前为止我做了:
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
System.out.println("0");
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
max++;
}
}
}
But the output I am getting is 但我得到的输出是
The "0" is there to show the spaces, but I want to remove the entire line which contains the first "0" so that the output starts with a "1". “0”用于显示空格,但我想删除包含第一个“0”的整行,以便输出以“1”开头。 I am unsure what to change.
我不确定要改变什么。 Any help would be much appreciated.
任何帮助将非常感激。 Thank You.
谢谢。
I suggest adding conditions (if we need to print out delimiters ): 我建议添加条件 (如果我们需要打印出分隔符 ):
for (int i = 1; i <= a; ++i) {
if (i > 1)
System.out.println(); // more than 1 line, need delimiter (new line)
for (int j = 1; j <= i; ++j) {
if (j > 1)
System.out.print(" "); // more than 1 column, need delimiter (space)
System.out.print(j);
}
}
Most shortest form: 最短的形式:
String str = "";
for (int i = 1; i <= 6; i++) {
str = str + " " + i;
System.out.println(str);
}
Output: 输出:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Here what I've got 这就是我所拥有的
Here you can check https://code.sololearn.com/c9ALHSGAa6ZZ 在这里,您可以查看https://code.sololearn.com/c9ALHSGAa6ZZ
class printPattern {
public static void main(String[ ] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for (i = 1; i <= a; i++) {
num = 1;
for (j = 1; j <= max; j++) {
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println();
max++;
}
}
}
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println(" ");
max++;
}
}
}
this is working as you asked. 这就像你问的那样有效。 just remove 0 from print statement
从print语句中删除0
How about this ? 这个怎么样 ?
public void pyramid(int size) {
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.