简体   繁体   English

在 Java 中创建此模式(嵌套循环)

[英]Make This Pattern in Java (Nested Loops)

The idea is that you input in a number to start at and gradually decrease the numbers.这个想法是你输入一个数字开始并逐渐减少数字。 Also, if you reach 9, the pattern starts back at one.此外,如果您达到 9,则该模式将从 1 开始。

Example output 1:示例输出 1:

11111 2222 333 44  5

11111 2222 333 44

11111 2222 333

11111 2222

11111

Example output 2:示例输出 2:

7777 888 99 1

7777 888 99

7777 888

7777

Edit: Sorry, I didn't really have code because I didn't know what I was doing, but even though the answer's code wasn't right, it helped me start.编辑:对不起,我没有真正的代码,因为我不知道我在做什么,但即使答案的代码不正确,它也帮助我开始。 Also, the numbers have to go down to 1 number at the end.此外,数字最后必须降到 1 个数字。

Here's my code right now that makes the first line:这是我现在的第一行代码:

 public String makePattern() { String output = ""; int amountPlace = amount; //amount is the imput int amount2 = amount; for(int i = 0;i < amount; i++) { for(int n = amount2; n > 0;n--) { output = output + amountPlace + ""; } output = output + " "; amountPlace++; amount2--; if(amountPlace==10) amountPlace=1; } return output; }

I still need help making the other lines.我仍然需要帮助制作其他线条。

why not try this then那为什么不试试这个呢

int numcount = 5;
string num_output = "";
for(int i = 0;i < 9; i++){
  for(int n = 0; n < numcount;n++){
    num_output += 'NUMBER TO APPEND';
  }
  numcount--;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM