Home > Blockchain >  Printing isoceles triangle based on user input
Printing isoceles triangle based on user input

Time:07-09

I can make my code print out the proper amount of asterisks, but I need the asterisks in the "row" plane to increment only once each line. Here is the current code:

package isoceles;

import java.util.Scanner;

public class Isoceles {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner stdIn = new Scanner(System.in);
        
        int userSpecified;//this will be the userSpecified triangle side size
        int triangleSide;//printed triangle side size
        int width, height;
        
        System.out.print("Enter the size of equal sides for isoceles triangle: ");
        userSpecified = stdIn.nextInt();
        
        for (int col=1; col<=userSpecified; col  ) 
        {
           for (int row=1; row<=userSpecified; row  ) 
           {
               System.out.print("*");
           }
           System.out.println("");
       }
   }
}

With an input of four, the output will look like this:

^^^^
^^^^
^^^^
^^^^

The desired output is:

^
^^
^^^
^^^^

CodePudding user response:

Your inner-loop is wrong, instead of row<=userSpecified, it should be row<=col. Try this:

import java.util.Scanner;

public class Isoceles {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner stdIn = new Scanner(System.in);
        
        int userSpecified;//this will be the userSpecified triangle side size
        int triangleSide;//printed triangle side size
        int width, height;
        
        System.out.print("Enter the size of equal sides for isoceles triangle: ");
        userSpecified = stdIn.nextInt();
        
        for (int col=1; col<=userSpecified; col  ) 
        {
           for (int row=1; row<=col; row  ) 
           {
               System.out.print("*");
           }
           System.out.println();
       }
   }
}
  •  Tags:  
  • java
  • Related