Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: for loop problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default for loop problem

    I need help in this java prob
    • Prompt user for new height of the triangle after the result was printed out
    • User can terminate the program by entering number, 0 and below


    /* Output
    Enter height of triangle: 3
    1 2 3
    1 2
    1
    Enter height of triangle: 2
    1 2
    1
    Enter height of triangle: -1
    */

    Attached is my incomplete program
    import java.util.*;
     
    public class Triangle {
    	public static void main (String[]args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Enter the height of triangle: ");
    		int height = sc.nextInt();
     
    		for (int num=height; num>0; num--) {
    			for (int dispNum=0; dispNum<num; dispNum++) {	
    				System.out.print(dispNum+1 +" ");
    			}
                            System.out.print("\n");
    		}
    	}
    }
    Last edited by helloworld922; October 8th, 2011 at 12:42 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: for loop problem

    What part do you need help with? Are you getting incorrect outputs? If so, what is wrong about them?

    One thing I can see right away is that you're not looping your program. My recommendation is to create a helper function which can print out a triangle given an integer height. Then in your main method use a while loop which repeatedly asks for the user to input a height. If the height is greater than 0, draw the triangle and continue the while loop. Otherwise, terminate the loop to exit the program.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    javaneedhelp (October 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: for loop problem

    i need help in shifting the prompting inside the loop.

    it will be great if explanation can be provided!

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: for loop problem

    The following control statement might be useful:
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. While loop problem
    By ak120691 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:09 PM
  2. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 12:12 PM
  3. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  4. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 11:23 AM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM

Tags for this Thread