-1

If 5 is inserted in sc.input below:

  *
 ***
*****

but, I got below:

   *
 ***
*****

What's the problem?

public void test3() {
    Scanner sc = new Scanner(System.in);
    int n1 = sc.nextInt();
    for(int i=0; i<n1; i++){
        if(i<=n1/2){
            for(int j=0; j<n1; j++) {
                if( j<(n1/2)-i || j>(n1/2)+i ) {
                    for(int m=0; m<(n1/2)-i; m++){
                        System.out.print(" ");
                    }
                    
                } /*else if( j==2 ) {
                    
                        System.out.print("*");
                    
                    
                }*/ else {
                    System.out.print("*");                       
                    
                }
            }
            System.out.println();
        }
    }
}
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
wi s
  • 1
  • 2
  • 4
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner Jul 27 '20 at 08:11
  • if( j<(n1/2)-i || j>(n1/2)+i ) { for(int m=0; m<(n1/2)-i; m++){ System.out.print(" "); } } in that part, it prints " " for two times , as expected since it enters the if statements for two times. You can remove this if statement if you do not want it. – fatma zehra güç Jul 27 '20 at 08:12
  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Elias Johannes Jul 27 '20 at 09:08
  • What is the expected behavior for even input numbers as `n1`? – Roberto Caboni Jul 27 '20 at 09:10
  • no expected. just ...row. and then finally, I will make reversed *s in first (else if) or (else) statement. – wi s Jul 27 '20 at 09:29

2 Answers2

0

You had some logic errors in your loop:

for(int i=n1/2; i>=0; i--)  // (#1)
{
    for(int j=0; j<n1; j++) {
        if( j < i || j>n1-i-1 ) { // (#2) 
            System.out.print(" "); // (#3)
        } else {
            System.out.print("*");                       
        }
    }
    System.out.println();
}

My remarks (each note refers to a specific code part, see (#numbers) in code comments):

  1. Why looping up to n1-1 and checking for n1/2? In this way you skip half of your iterations, an that's pointless. Just loop up to n1/2 so that you can avoid that if-conditional.
    Even better: let's count backyards; it will be useful later

  2. We need a brand you condition in order to understand when a space has to be printed.

    Logic: in the first rows (high i) we need to print more spaces. As i is decremented we will have less and less spaces (until, in the last row, we will have 0 of them). You just need to build a range:

     if( j < i || j>n1-i-1 )
    

    the last -1 in j>n1-i-1 is required because the range is from 0 to n1-1

  3. After we found the correct condition on j for printing space, we are fine. So we don't need a forloop for printing spaces anymore


Please note how this logic works also for even values of n1.

Output for n1=7:

   *   
  ***  
 ***** 
*******

Output for n1=8:


   **
  ****
 ******
********
starball
  • 20,030
  • 7
  • 43
  • 238
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
0

I noticed that each line printed has an odd number of *. Is the user input to collect the number of lines, or is it to collect the maximum number of * in the last line? If the input collect the number of lines to print to the screen, then you can do it as shown in the code below.

import java.util.Scanner;

/**
 * Output:
 *
 * Enter the number of rows: 10
 *              *
 *             ***
 *            *****
 *           *******
 *          *********
 *         ***********
 *        *************
 *       ***************
 *      *****************
 *     *******************
 *
 *
 * @author martinfall
 */
public class Test {

    /**
     * Main method.
     *
     * @param args
     */
    public static void main(String[] args) {
        // Create a new Scanner object
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter the number of row 
    System.out.print("Enter the number of rows: ");
    int number = input.nextInt(); // number holds the number of rows

    // Outer loop prints the number of lines entered by user
    for (int i = 0; i < number; i++) {
        // Pad each line with the necessary blank spaces
        for (int j = 0; j < Math.ceil(number - i); j++) {
            System.out.print(" ");
        }

        // Print an odd number of * on each line, such as 1, 3, 5, etc.
        for (int k = 0; k < (2 * i) + 1; k++) {
            System.out.print("*");
        }

        // Print a new line at the end of each line of *
        System.out.println();
        }
    }
}

If the input is to collect the number of * in the last line, then it gets a little tricky to balance the odd/even lines of *

Martin Fall
  • 121
  • 7