Печать * как стрелка в Java - PullRequest
0 голосов
/ 31 мая 2018

Итак, я сделал свой лучший снимок, и я действительно ломаю голову, пытаясь найти решение для решения этой проблемы.Этот тип вопроса уже задавался здесь ранее, но на этот раз с изюминкой.

Так что я должен напечатать стрелку, указывающую влево, такую ​​как:

  public class Arrow {

  public static void main(String[] args) {

  int n = 0;

  if (args.length < 1) {

     System.out.println("Input a value.");
     System.exit(0);  

  }      
  else {

     n = Integer.parseInt(args[0]);

  }

  for (int rows = 1; rows <= n; rows++) {//This tells how many lines to print (height)

    for (int numSpaces = 0; numSpaces < (n - rows); numSpaces++) {//Prints spaces before the '*'
        System.out.print("  ");
    }

    for (int numStars = 0; numStars < rows; numStars++) { //Prints the " " first in each line then a "*".  
        System.out.print("*");
    }

    System.out.println(""); //Next Line         

  }

  for (int rows = 1; rows <= n; rows++) {//This tells how many lines to print (height)

    for (int numSpaces = n; numSpaces > (n - rows); numSpaces--) {//Prints spaces before the '*'
        System.out.print("  ");
    }

    for (int numStars = n; numStars > rows; numStars--) { //Prints the " " first in each line then a "*".  
        System.out.print("*");
    }

    System.out.println(""); //Next Line         

    }       

 }

}

        *
       **
      ***
     ****
    *****
     ****
      ***
       **
        *

ТеперьПроблема, с которой я сталкиваюсь, заключается в том, что стрелка должна иметь средний кусок, торчащий из задней части, эквивалентной длине n + (n - 1).Я не могу понять, как это сделать, потому что я новичок в лупах, и я потратил большую часть 2 часов, пытаясь сделать это правильно.

Может какая-то добрая душа, пожалуйста, пожалуйста, помогите мне помочьlol.

Спасибо

1 Ответ

0 голосов
/ 31 мая 2018

Я только что добавил блок if в ваш первый большой цикл for.Ищите // This is where the tail of the arrow starts и // This is where the tail of the arrow ends.Сейчас я сохранил длину хвоста как 2*n, вы можете изменить его в соответствии со своими потребностями, скажем, 3*n или что-то еще.

Проверка if помогает оценить состояние и предпринять соответствующее действие:

  for (int rows = 1; rows <= n; rows++) {//This tells how many lines to print (height)

    for (int numSpaces = 0; numSpaces < (n - rows); numSpaces++) {//Prints spaces before the '*'
        System.out.print("  ");
    }

    for (int numStars = 0; numStars < rows; numStars++) { //Prints the " " first in each line then a "*".  
        System.out.print("*");
    }
    // This is where the tail of the arrow starts
    if(rows==n) { 
        // Decide the length of the tail of the arrow here. It is currently 2*n below
        for(int arrowLength = 1; arrowLength <= 2*n; arrowLength++) {
            System.out.print("*");
        }
    }
    // This is where the tail of the arrow ends
    System.out.println(""); //Next Line         
  }

Вывод с вводом как 10:

                  *
                **
              ***
            ****
          *****
        ******
      *******
    ********
  *********
******************************
  *********
    ********
      *******
        ******
          *****
            ****
              ***
                **
                  *
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...