samedi 9 septembre 2023

Triangle Digit ouput [closed]

I am currently trying to make a method that displays a triangle or pyramid. This special triangle counts the frequency of each number in the previous row. the method takes two inputs but has no parameters since I am creating a "pyramid" object. I have attached an example of the correct output. My current method has the correct digit combination but I am not able to space each digit and make it into a pyramid shape. enter image description here. I apologize for any formatting mistakes or any other issues. This is my first post on stack overflow. The desired output is enter image description here

here is my code so far and I have attached an image of my output: an image of my code:

  public void printpyramid() 
  {

   String ndig= String.valueOf(digit);
   String cdig="";
   int runl=1;

   int cumuldigits=0;
   while(runl<=numrows){
      cumuldigits=0;
   
      System.out.println(ndig + "");

    for(int index =0; index<10;index++)
     {
         if(ndig.charAt(i)==(char)(index+'0'))
         {   
         digcounter[index]=digcounter[index]+1;
          }
     }
  }
    for(int index =0; index<10;index++)
     {
       if(digcounter[index]>0)
        {
        cdig = cdig + String.valueOf(digcounter[index]) + 
        String.valueOf(index);
        cumuldigits++;
        }
      }
  if(cumuldigits>=10)
  {
    break;
   }
  if(cdig.equals(ndig))
  {
   System.out.println(ndig);
  break;
  }
  runl++; 
  ndig=cdig;
  cdig="";
}
 }

Aucun commentaire:

Enregistrer un commentaire