英文:
printing out the opposite of a ladder in a loop
问题
以下是翻译好的内容:
我想要打印出这个图案,但是我在使用while循环或for循环时遇到了问题。
#######
#####
###
#
但是我的带有while循环的代码打印出了这个图案:
#
##
###
####
#####
我不知道如何将它反向打印出来,有人可以帮帮我吗?
public class oppositeloop {
public void oppositelad() {
int g = 1;
char f = '#';
while (g <= 5) {
int r = 1;
while (r <= g) {
System.out.print(f);
r += 1;
}
g += 1;
System.out.println();
}
}
public static void main(String[] args) {
oppositeloop n = new oppositeloop();
n.oppositelad();
}
}
英文:
I want to print out this but I am having trouble with it using while loops or for loop.
#######
#####
###
#
But my code with the while loop prints out this
#
##
###
####
#####
I don't know how to print it backwards could someone help me.
public class oppositeloop
{
public void oppositelad(){
int g = 1;
char f = '#';
while (g <= 5){
int r= 1;
while(r <= g){
System.out.print(f);
r += 1;
}
g += 1;
System.out.println();
}
}
public static void main(String[] args) {
oppositeloop n = new oppositeloop();
n.oppositelad();
}
}
答案1
得分: 0
尝试:
// 要打印多少行
for (int i = 4; i > 0; i--) {
// 在该行中要打印多少个'#'
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("#");
}
// 到下一行
System.out.print("\n");
}
英文:
Try:
// How many lines to print
for (int i = 4; i > 0; i--) {
// How many '#' to print in that line
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("#");
}
// To next line
System.out.print("\n");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论