1

Trying to print pattern like half sphere below i have added actual output and expected output along with my code can anyone help how to do it. Thanks in advance

My code

public class PatternHalfSphere {

    public static void main(String[] args) {
        int i,j;
        for(i = 1;i<=4;i++){
            System.out.println();
            for(int k=3;k>=i;k--){
                System.out.print(" "+"*"+" ");
            }
            for(j=1;j<=i;j++){

                System.out.print("   ");
            }
        }
        for(int k=0;k<=3;k++) {
            for(int l = 0; l<k;l++) 
            {
                System.out.print(" "+"*"+" ");
            }
            System.out.println();
        }
    }


}

Actual output

 *  *  *    
 *  *       
 *          

 * 
 *  * 
 *  *  * 

Expected output

     *  *  *    
     *  *       
     *                 
     *  * 
     *  *  * 
BlueWookie
  • 55
  • 1
  • 3
  • 12
  • 1
    So basically you would like to shift the image 1 position to the right? – dbl Sep 10 '18 at 14:58
  • 1
    I'd take a stab at doing some actual debugging first: play computer with a pen/cil and paper. Trace what it's doing. Step through the code. Reason about what's happening. – Dave Newton Sep 10 '18 at 14:58
  • Your result looks ok with 2 exceptions: it needs to be indented (unless that's just a formatting issue in your question) and the single star line is double. It shouldn't be too hard to change both - as Dave said try it on paper (pretend you're the computer). – Thomas Sep 10 '18 at 15:00

3 Answers3

1

This answer takes your exact code, and with a few changes arrives at the output you expect. You were close, and you only needed to print a spacer on each line, and also cut one iteration off your second outer for loop to avoid printing * twice.

for (int i=1;i <= 3; i++) {
    for (int k=3; k >= i; k--) {
        // print a new spacer at the start of each line
        if (k == 3) System.out.print("   ");
        System.out.print(" " + "*" + " ");
    }
    for (int j=1; j <= i; j++) {
        System.out.print("   ");
    }
    System.out.println();
}

// start at k=2 so as to NOT print a double single asterisk *
for (int k=2; k <= 3; k++) {
    for (int l=0; l < k; l++) {
        // print a new spacer at the start of each line
        if (l == 0) System.out.print("   ");
        System.out.print(" "+"*"+" ");
    }
    System.out.println();
}

    *  *  *    
    *  *       
    * 
    *  * 
    *  *  * 

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Quick and dirty solution

 public static void main(String[] args) {
        upperHalf(4);
        bottomHalf(4);
    }

    private static void upperHalf(int size) {
        for(int row = 0; row<size; row++){
            String rowContent = "";
            for(int col=0; col<size-row; col++){
                rowContent+= " *";
            }
            if(!rowContent.equals(""))
                System.out.println(rowContent);
        }
    }

    private static void bottomHalf(int size) {
        for(int row=2; row<=size; row++) {
            String rowContent = "";
            for(int col=0; col<row;col++) 
            {
                rowContent+= " *";
            }
            System.out.println(rowContent);
        }
    }
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
  • This is the exact solution, i am curious to learn and try like you bro, is there any other websites you can tell me where i can get questions like this to practice more on my own as i am getting home works from my lecturer alone so after completion i can try getting questions from websites –  Sep 10 '18 at 15:29
0

Firstly, initialize k with the rest of the variables.

int i, j, k;

Then you should decide which for-loop will be responsible for the single '*' printed and adjust the other accordingly. For example, if you terminate the first loop 1 step sooner it should fix the gap between the 2 parts.

Now I am keeping the single '*' for the first for-loop and skipping it in the second one by modifying the steps.

  1. Initializing k=2 instead of k=0. Fixing the single * repetition along with the space between them.
  2. removed completely the for-loop that used j as a counter cause it was messing with the spacing in the output printing an unwanted * further away.
  3. finally added a System.out.println() before the second for-loop so that the * printed by the second for-loop will begin from a new line.

    import java.util.*;
    import java.lang.*;
    
    class Rextester
    {  
        public static void main(String args[])
        {
            for (int i=1;i <= 3; i++) {
                System.out.println();
                for (int k=3; k >= i; k--) {
                    System.out.print(" " + "*" + " ");
                }
            }
            System.out.println();
            for (int k=2; k <= 3; k++) {
                for (int l=0; l < k; l++) {
                    System.out.print(" "+"*"+" ");
                }
                System.out.println();
            }
        }
    }
    

Ultimately this is a problem solved by tinkering with your numbers or by pen and paper problem solving before even writing code.

BlueWookie
  • 55
  • 1
  • 3
  • 12