我要在 for 循环上制作以下模式:
XXXXXXXXXX
XXXXXXXXXY
XXXXXXXXYY
XXXXXXXYYY
...
..等等
public class ex{
public static void main(String[] args){
for(int i=0;i<=10;i++){
System.out.println();
for(int j=0;j<=10;j++){
if(i==0){
System.out.print("X");
}
if(i==1){
System.out.print("X");
if(j==9){
System.out.print("Y");
}
}
}
}
}
} 〜
我在输出末尾得到了我不想要的额外“X”。 我认为有更好的方法来做到这一点,但现在想不出办法
大家有什么帮助吗?
最佳答案
尝试在一个循环内嵌套两个循环。计数到 i
,然后在外循环的每次迭代中继续计数到 10
:
// 10 lines
for(int i = 10; i >= 0; i--){
int j = 0;
// Print 'X's (10 - i of them)
for(; j < i; j++)
System.out.print("X");
// Print 'Y's (i of them)
for(; j < 10; j++)
System.out.print("Y");
System.out.println();
}
关于java - 需要有关简单 Java 模式编码的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922715/