CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2011
    Posts
    5

    Red face Help with printing stars

    hey, I can't get this program working;
    public class Uppgift2 {



    static void main(String[] args) {
    System.out.println("My Star Triangle");
    printChar('+', 16);
    System.out.println();
    for (int i = 0; i < 16; i++) {
    printChar(i, 16 - i);
    System.out.println();
    }

    }


    static void printChar (int start, int width){
    for (int i = 0; i<bredd; i++){
    printStars(' ', start);
    printStars('*', width);
    System.out.println(" ");


    }

    return;



    }
    static void printStars(char Stars, int number){

    for(int i = 0; i<number; i++){

    System.out.print(Stars);
    }

    }
    }

    No errors are visible but nothing comes up in the console. I want the stars to have the formation like this:
    My Star Triangle
    ++++++++++++++++
    ****************
    **************
    ************
    **********
    ********
    ******
    ****
    **
    the triangle is meant to be symetric on both sides, like an upside-down pyramid.
    (I would appreicate if anyone could help me out here)
    best regards Emil
    Last edited by NeWoX; February 21st, 2011 at 07:11 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with printing stars

    Please use code tags when posting code.

    Code:
    No errors are visible but nothing comes up in the console.
    Really. It doesn't even compile on my system. The printChar() method contains an undeclared variable 'bredd'. And even when you fix this you get error when you try to run it because there isn't a public static main method.

    As for your code it has a number of logic errors in it so you need to step through it by hand and write down on paper what it is doing as you go.

    BTW why name a method 'printStars' when it prints the char that is passed in which isn't necessarily a star?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Feb 2011
    Posts
    5

    Re: Help with printing stars

    I still don't get it, I've tried to write down a psedo code but with no success. I can't find the logical error (i'm no "prog" pro ) either as Keang mentioned, except for the obvious ones.

    The outprinted triangel should look something like this;
    ++++++++++++++++
    ****************
    **************
    ************
    **********
    ********
    ******
    ****
    **
    here's the code;
    Code:
    public class Uppgift2 {
    
    	public static void main(String[] args) {
    		System.out.println("My Star Triangle");
    		printChar('+', 16);
    		System.out.println();
    		for (int i = 0; i < 16; i++) {
    			printChar(i, 16 - i);
    			System.out.println();
    
    		}
    
    	}
    
    	static void printChar(int start, int width) {
    		for (int i = 0; i < width; i++) {
    			printStars(' ', start);
    			printStars('*', width);
    			System.out.println(" ");
    
    		}
    
    		return;
    
    	}
    
    	static void printStars(char Stars, int number) {
    
    		for (int i = 0; i < number; i++) {
    
    			System.out.print(Stars);
    
    		}
    
    	}
    }
    I have no idea how to get the + characters on the top aswell as the symmetry. I would appreciate if someone could help me.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with printing stars

    You don't need to be an expert to work out what is wrong, you just need to look at the output and apply a little deduction.

    Debugging software is something that needs to be learned in the same way that you need to learn how to write software. For a start focus on just one problem at a time, preferably the first problem that appears (fixing it may solve other problems). Look at the output and compare it to what you expected. Look at the code and work out why it is producing the output you are seeing. Only when you understand what the code is actually doing apply a fix to it.

    For example:
    First a block of stars appears when it is supposed to plus characters - follow the code and find out why when you pass a '+' in you are getting '*' out.

    Next when you are printing out a line the stars you get many lines and each subsequent block of stars has one less line than the previous block. Why, when you ask for one line of n stars is it printing several lines (maybe you have too many for loops).

    Finally why is the triangle not displaying properly ie why are all the spaces at the beginning of the line and why does each lime have 1 less star than the previous line when your example diagram has 2 less stars.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured