Java 我打印循环的逻辑不正确。我的头绕不过去了

Java 我打印循环的逻辑不正确。我的头绕不过去了,java,Java,//问题本身(之后是我的代码) 变量n是随机生成的整数。输出字符“*”和“#”,以便第一行仅包含星号,最后一行仅包含数字符号。在每一连续的行中,星星的数量都会减少。一行中的字符总数为n,共有n+1行 例如,如果n的值为5,程序将输出: ***** ****# ***## **### *#### ##### //下面是我的代码 Random r = new Random(); int n = r.nextIn

//问题本身(之后是我的代码)

变量n是随机生成的整数。输出字符“*”和“#”,以便第一行仅包含星号,最后一行仅包含数字符号。在每一连续的行中,星星的数量都会减少。一行中的字符总数为n,共有n+1行

例如,如果n的值为5,程序将输出:

 *****  
 ****#    
 ***##      
 **###       
 *####      
 #####      
//下面是我的代码

Random r = new Random();       
int n = r.nextInt(5) + 10;    
System.out.println("n: "+n);

while(n>0){

for(int star = n; star>0; star--){
  System.out.print("*");
}

for(int hash = 0; hash<n; hash++){
  System.out.print("#");

    } 

    System.out.println("");  //new line
n--;


}

您当前正在每个迭代中打印正确数量的
*
s。您应该打印的
#
的数量始终等于您正在进行的迭代(从0开始)。因此,您可以保留此计数器(例如,
i
),它将在每次
循环迭代时初始化为0并在每次
结束时递增一次,然后您可以通过循环到
i
来打印
#

for (int hash = 0; hash < i; hash++) {
    System.out.print("#");
}

您的
star
循环开始时还可以,但在
hash
循环中,您告诉您的程序只需倒数到
n
并打印那么多散列标记即可


相反,您需要另一个数字来表示星星数或散列数;从
n
中减去以得到另一个数字。对
循环使用嵌套的
;外部的一个计数到
n
以打印每一行,内部的一个计数从0到
lineNumber
打印星号,然后一直到
n
打印散列。

请记住您所在的行。不需要算法对话

final int n = 4; 

int lineNr = 0;

while (n >= lineNr)
{
    for (int i = 1; i <= n - lineNr; i++)
        System.out.print("*");

    for (int j = 1; j <= lineNr; j++)
        System.out.print("#");

    System.out.println();
    lineNr++;
}
final int n=4;
int-lineNr=0;
而(n>=lineNr)
{

对于(int i=1;i不要在循环中修改n,因为n给出了每行的符号总数。这里我保留了您的程序,并引入了变量i以在while循环中使用

Random r = new Random();       
int n = r.nextInt(5) + 10;    
System.out.println("n: "+n);
int i=n;

while(i>=0){

  for(int star = i; star>0; star--){
    System.out.print("*");
  }

  for(int hash = i; hash<n; hash++){
    System.out.print("#");
  } 

  System.out.println("");  //new line
  i--;
}
Random r=new Random();
int n=r.nextInt(5)+10;
System.out.println(“n:+n”);
int i=n;
而(i>=0){
对于(int-star=i;star>0;star--){
系统输出打印(“*”);
}
对于(int hash=i;hash我的代码:

Random r = new Random();       
int lines = r.nextInt(5) + 10;
System.out.println("n: "+lines);

for (int n=lines; n>=0; n--) {
  String toPrint = "*";
  for(int i=0; i<lines; i++){
    if (i==n) {
      toPrint = "#";
    }
    System.out.print(toPrint);
  }
  System.out.println();
}
Random r=new Random();
内线=r.nextInt(5)+10;
System.out.println(“n:+行);
对于(int n=行;n>=0;n--){
字符串toPrint=“*”;

例如(inti=0;我喜欢做家庭作业,但他做了一次不错的尝试。我愿意提供一些提示(尽管不是所有的代码!)。@Janesabouchleichthanks@Will不客气。-您的
while
条件有缺陷。首先,它是
while(i>0)
,没有
int
。其次,它实际上是
while(i>=0)
,打印最后一行。除此之外,它还能工作。
Random r = new Random();       
int n = r.nextInt(5) + 10;    
System.out.println("n: "+n);
int i=n;

while(i>=0){

  for(int star = i; star>0; star--){
    System.out.print("*");
  }

  for(int hash = i; hash<n; hash++){
    System.out.print("#");
  } 

  System.out.println("");  //new line
  i--;
}
Random r = new Random();       
int lines = r.nextInt(5) + 10;
System.out.println("n: "+lines);

for (int n=lines; n>=0; n--) {
  String toPrint = "*";
  for(int i=0; i<lines; i++){
    if (i==n) {
      toPrint = "#";
    }
    System.out.print(toPrint);
  }
  System.out.println();
}