Java Seng Basic Question (Method, make a triangle with a for statement) (Please help me...)

Asked 2 weeks ago, Updated 2 weeks ago, 2 views


import java.util.Scanner;

class Recursion{
    //method

    public void star(int x){
        if(x>0){
            printBlank(x);
            printStar(x);
            System.out.print("\n");
            star(x-1);
        }
        else
            return;     
    }

    public void printBlank(int x){
            //
        for(int i=0;i<x-1;i++)
            System.out.print(" ");
    }
    public void printStar(int x){
            //I want a code that prints stars with the number of escalations in the for statement.
            // 1 in the first row, 3 in the second row, 5 in the third row...
        System.out.print("*");
    }
}

public class Assignment10
{
    public static void main(String[] args) 
    {


        System.out.println ("Enter a number");
        Recursion rec = new Recursion();

        Scanner scan = new Scanner(System.in);
        int input = scan.nextInt();

        rec.star(input);

       /*    Without a recusive...Implementation

        for(int i=0;i<input;i++){
            for(int j=i; j<input-1;j++)
                System.out.print(" ");
            for(int j=0;j<2*i+1;j++)
                System.out.print("*");
            System.out.print("\n");
        */

    }
}

I was practicing recursive, but I was blocked from specifying the number of stars.

recursive for method

2022-09-20 11:12

1 Answers

        if(x>0){
            star(x-1);
            System.out.print("\n");
            printBlank(x);
            printStar(x);
        }


2022-09-20 11:12

If you have any answers or tips


© 2022 pinfo. All rights reserved.