Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java program

Options
  • 08-05-2006 6:37pm
    #1
    Registered Users Posts: 447 ✭✭


    Sorry for being such a lazy so and so but im really stuck for time with exams coming up so if an one can help with with this program it would be appreciated. here is the task

    Write a program that will write out a wedge of stars. The user will enter the initial number of stars, and the program will write out lines of stars where each line has one few star than the previous line:

    Initial number of stars:
    7

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

    cheers


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    This thread has 12 hours to live.

    Post up the work that you've done thus far and explain what part is giving you trouble, or this thread will be locked.

    Getting other people to do your homework for you is against the charter.


  • Registered Users Posts: 979 ✭✭✭Keedowah


    Sorry - but you would have done something very similar to this in your 2nd or 3rd lecture - look at the notes.

    In fact you would have probably been given this in your first / second practical class.

    tbh its very basic and if you dont put the effort into solving this yourslef now - you'll have a lot of trouble next year.

    sorry...


  • Registered Users Posts: 11,747 ✭✭✭✭wes


    I have to agree with the previous poster, this is stuff is really basic and you should be able to hammer it out in a few minutes. Also I reckon if you looked online you could find something similar.


  • Registered Users Posts: 447 ✭✭blocparty


    this is what ive done thus far:

    import java.io.*;


    class stars
    {
    public static void main (String[] args ) throws IOException
    {

    int numStars; // the number of stars
    int base;
    int star;

    BufferedReader userin = new BufferedReader (new InputStreamReader(System.in));
    String inputData;

    System.out.println( "How many stars in the initial row?" );
    inputData = userin.readLine();
    numStars = Integer.parseInt( inputData );



    star = 1
    while ( star <= numStars )
    {
    System.out.print("*");
    star = star + 1;
    }
    numStars - 1
    while (numStars >= base)

    while ( star <= numStars )
    {
    System.out.print("*");
    star = star + 1;

    }
    }
    }


    the problem i am having is getting the initial number of stars to decrease by one for each row down to the base.

    any help?

    sorry bout not posting my attempt


  • Registered Users Posts: 11,747 ✭✭✭✭wes


    import java.io.*;
    
    class temp
    {
    	public static void main (String[] args ) throws IOException
    	{
    		int numStars; // the number of stars
    		int base = 0;
    		int star;
    		int temp = 0;
    
    		BufferedReader userin = new BufferedReader (new InputStreamReader(System.in));
    		String inputData;
    
    		System.out.println( "How many stars in the initial row?" );
    		inputData = userin.readLine();
    		numStars = Integer.parseInt( inputData );
    		
    		for(int i = 0; i < numStars; i++){			
    			for(int j = 0; j < (numStars - i); j++) {
    				System.out.print("*");
    			}
    			System.out.println("");
    		}
    
    	}
    }
    

    Okay I swapped out your while loops. I don't know why you had 3. Anyway it is much easier to use for loops for something like this and the 2 nested for loops do the job.

    The code runs just fine under Java 5 btw. You just need to clean up the code a bit.


  • Advertisement
  • Registered Users Posts: 447 ✭✭blocparty


    ya sorry there should have only been 2 while statements.
    we havent actually done for statements yet so im afraid to say that im lost about your solution. no idea where the i the j and the j++ came from but thanks for the help anyway


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    I think you need to invert your for loop
    for(int i = 0; i < numStars; i++){			
    

    will result in:
    *
    **
    ***
    ****
    etc.

    while I think the original idea was to do it like this:
    *****
    ****
    ***
    **
    *

    which needs
    for(int i = numStars; i > 0; i--){	
    


  • Registered Users Posts: 447 ✭✭blocparty


    i dont understand the for statement cause we havent studied it yet thats why i used the while statements so if anyone could help me using the while statements it would be appreciated


  • Registered Users Posts: 11,747 ✭✭✭✭wes


    bpmurray wrote:
    I think you need to invert your for loop
    for(int i = 0; i < numStars; i++){			
    

    will result in:
    *
    **
    ***
    ****
    etc.

    while I think the original idea was to do it like this:
    *****
    ****
    ***
    **
    *

    which needs
    for(int i = numStars; i > 0; i--){	
    

    I nested the loops so it does work the way the op wanted. I tested the code and everything. The second for statement takes care of it.


  • Registered Users Posts: 11,747 ✭✭✭✭wes


    blocparty wrote:
    ya sorry there should have only been 2 while statements.
    we havent actually done for statements yet so im afraid to say that im lost about your solution. no idea where the i the j and the j++ came from but thanks for the help anyway

    The j++ is the same as j = j + 1. Its just a shorter way to do it.

    As for the for loop, you can look here and here:

    This one is specific to java:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

    More general one:
    http://en.wikipedia.org/wiki/For_loop

    The logic I used above should still work in a while loop, but its a bit harder to get working.


  • Advertisement
  • Registered Users Posts: 2,932 ✭✭✭Sniipe


    this will work for you. I know its the day before handup but its important that u understand it. You almost had it right but you needed to put one loop inside another. The first loop will go through the lines. The second loop will go through putting in a * on that line.
    import java.io.*;
    class temp
    {
     public static void main (String[] args ) throws IOException
     {
      int numStars; // the number of stars
      int star = 0;
      int var = 0;
      BufferedReader userin = new BufferedReader (new InputStreamReader(System.in));
      String inputData;
      System.out.println( "How many stars in the initial row?" );
      inputData = userin.readLine();
      numStars = Integer.parseInt( inputData );
      
                    while (star < numStars)
                    {                     
                        while (var < (numStars - star))
                        {                        
                            System.out.print('*');                        
                            var = var + 1;
                        }
                        var =0;
                        System.out.println("");
                        star = star + 1;                          
                    }               
     }
    }
    
    note the System.out.print('*'); line and where var=0;


  • Registered Users Posts: 447 ✭✭blocparty


    cheers thanks for all the help guys.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    i dont understand the for statement cause we havent studied it yet thats why i used the while statements so if anyone could help me using the while statements it would be appreciated
    How could you NOT have studied for loops? There is not way in hell that in ANY course you covered a while loop but NOT a for loop.


  • Registered Users Posts: 447 ✭✭blocparty


    well believe it! im only in first year and the computer part of my degree is relatively small so i havent studied for loops just yet! maybe next year!


  • Closed Accounts Posts: 6 Software Guru


    Surely if you understand a While look you will be able to bring your intelligence to bear on a For loop. Come on! Use your brain.


  • Registered Users Posts: 447 ✭✭blocparty


    im sure if i spent a few minutes on it ill be able to understand it alrite but sadly it wouldnt be much use for me in terms of the work im doing at the moment because we havent done them and an exam is based on the work you've done!


Advertisement