dimanche 25 février 2018

What is the logic behind "if" statements? Drawing with for-loops

So I'm supposed to draw a mountain using a for-loop, using if statements to decide what character to use. This what I did initially.

for (int y = intMaxHeight; y >= 0; y--){
           for (int x = 0; x < arr.length; x++){
             double height = arr[x];

              if (y == 0){
                   System.out.print("-");
               } 

              if (((y - height) < 1) && ((y - height) > -1)){
                   System.out.print("^");
               } 

               if (y > height){
                   System.out.print(" ");
               }

               if ( y < height){
                   System.out.print(symbol);
               }       
           }
           System.out.println();
     }

This prints out a very ugly mountain This prints out a very ugly mountain Now, by adding "else" to the last three statements, the mountain looks like what it's supposed to, and I wonder why is that. enter image description here

But the new problem is that the mountaintop symbol "^" will be printed twice sometimes, which is not supposed to happen. I tried switching the order of the if statement, but just like with the ugly mountain, it didn't change anything. Any ideas on what the problem could be?

Aucun commentaire:

Enregistrer un commentaire