N
N
Nikita Salnikov2022-02-17 13:31:21
Dart
Nikita Salnikov, 2022-02-17 13:31:21

How to draw a triangle in the console?

Hello. Tell me how you can display in the console such a triangle
620e23248284a051966955.png

on the left side brought out in this way

for(var i = 0; i < width; i++){
    for(var j = 0; j < height; j++){
      if(i == 0 || i == height){
        sim += '*';
      print(sim);
      }
      
    }
    
  }


as in the picture it turned out like this
for(var a = 0; a < width; a++){
    for(var b = 0; b < height; b++){
      if(a == 0 || a == width){
        sim += '*';
      print(sim.padLeft(height));
      }
      
    }
    
  }


But is it possible to do this without padLeft?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2022-02-18
@Malcavion

this is how I did it,
I had the task to display any number that the user enters, the floors of the tree

void main() {
  buildTree();
}

void buildTree() {
  print('Введите количество этажей :');
  var input = stdin.readLineSync();
  var inputs = int.parse(input!);

  var b = "*";
  var branch = [b];

  void addBranch() {
    final branch1 = branch[0];

    if (inputs > 0) {
      print(branch1.padLeft(inputs-- ));

     for(int i = inputs; i > 0; i--){
       var tspaces = "".padLeft( --inputs );
       var tree = branch[0] += "**";
       print(tspaces+tree);

     }




    } else {
      print("!!!");
    }
  }

  for (var br in branch) {
    addBranch();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question