Results 1 to 11 of 11
  1. #1
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default pls help me fast!

    i need this to be done in Thursday here in my time,(Philippines) can somebody help me?
    i need to display this:(based on user input)
    user input=4
    a.
    *
    **
    ***
    ****
    ***
    **
    *
    user input=5
    b.* * * * * * * * * *
    * * * * * * *
    * * * * *
    * * *
    *
    c. user input =3
    *
    * *
    * * *
    * *
    *

    sorry for the bad drawing...
    i did one triangle that's in the letter a, here is my code.
    import java.io.*;

    class Triangle{
    public static void main(String args[]) {
    InputStreamReader is=new InputStreamReader(System.in);
    BufferedReader bf=new BufferedReader(is);

    int ans,size=0;
    char ch='*';
    try{
    System.out.println("Please choose:");
    System.out.println("1.triangle in the left");
    System.out.println("2.triangle in the middle");
    System.out.println("3.triangle in the right");
    ans=Integer.parseInt(bf.readLine());
    System.out.print("Enter an integer from 1 to 50 to indicate the size of the triangle: ");
    size = Integer.parseInt(bf.readLine());


    if(ans==1){
    for(int r=1; r <= size ; r++){
    for(int m = 1 ; m <= r ; m++){
    System.out.print(ch);
    }
    System.out.println();

    }

    // lower part
    for(int r= size-1; r >=1 ; r--){
    for(int m = 1 ; m <= r ; m++){
    System.out.print(ch);
    }
    System.out.println();
    }
    System.exit(1);
    }


    }catch(Exception e){}
    }
    }
    thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Does your code compile and execute?
    What does it output now?
    If the code is not doing what you want, try debugging it.
    Add some println() statements to show the values of r and m etc so you can see what is happening.

  3. #3
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    What do you need help with exactly? The other two options? What have you come up with so far? Does it work as expected? What errors do you get, if any?

    No one's gonna do the whole thing for you.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    And what about the others? We're not going to do them for you.

  5. #5
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default

    i got no errors with this, i just need the loop or set of codes for the two options, pls help me,. i got all that i can with this...

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Sorrry, it doesn't work that way

    This forum doesn't work that way. You need to do the work. The forum will help you when you get stuck on a certain part of your program and you provide enough infromation to be helped. If you need help on looping, there are different ways to do it in Java. Check this:

    java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html
    java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

    Hope this helps.
    CJSL

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Can you think thru the problem?
    One each line output a number of *
    The first line 1 *
    the second line 2 *
    etc until the max # of *
    then output the max # - 1
    then the max - 2
    ... down to 0

    Take a piece of paper and write down the the patterns for 3. How many lines of output does it take?
    *+**+***+**+* => 5 lines of output
    Do it again for 4 and 5 and 6 until you see the pattern.
    Now you can compute the number of times to go thru the outer loop. Now work on the inner loop(s) that outputs different number of *s

  8. #8
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default

    import java.io.*;

    public class TriangleCreator
    {
    public static void main(String[] args)
    {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    int vChoice=0;
    int num=0;

    do
    {
    try
    {
    System.out.println("CHOOSE TRIANGLE TYPE:");
    System.out.println("[1]--Left Aligned Triangle");
    System.out.println("[2]--Right Aligned Triangle");
    System.out.println("[3]--Center Aligned Triangle");
    System.out.println("[4]--Exit\n");

    System.out.print("Input Choice: ");
    vChoice = Integer.parseInt(bf.readLine());


    if(vChoice == 1)
    {
    System.out.print("\nHow many lines? (3-79): ");
    num = Integer.parseInt(bf.readLine());

    if(num<3 || num>79)
    {
    System.out.println("\nInvalid Number! Please enter 3-79 only...\n");
    }
    else
    {
    System.out.println();
    for(int i=1;i<=num;i++)
    {
    for(int j=1;j<=i;j++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    System.out.println();
    }
    }
    else if(vChoice == 2)
    {
    System.out.print("\nHow many lines? (3-79): ");
    num = Integer.parseInt(bf.readLine());

    if(num<3 || num>79)
    {
    System.out.println("\nInvalid Number! Please enter 3-79 only...\n");
    }
    else
    {
    System.out.println();
    for(int i=num; i>=1; i--)
    {
    for(int j=1; j<=num; j++)
    {
    if(j>=i)
    {
    System.out.print("*");
    }
    else
    {
    System.out.print(" ");
    }
    }
    System.out.println();
    }
    System.out.println();
    }
    }
    else if(vChoice == 3)
    {

    System.out.print("\nHow many lines? (3-39): ");
    num = Integer.parseInt(bf.readLine());

    if(num<3 || num>39)
    {
    System.out.println("\nInvalid Number! Please enter 3-39 only...\n");
    }
    else
    {
    System.out.println();
    for(int i=1;i<=num;i++)
    {
    for(int j=num;j>=i;j--)
    {
    System.out.print(" ");
    }
    for(int k=1;k<=i;k++)
    {
    System.out.print("*");
    }
    for(int k=1;k<i;k++)
    {
    System.out.print("*");
    }
    System.out.print("\n");
    }
    System.out.println();
    }
    }
    else if(vChoice == 4)
    {
    System.out.println("\nGoodbye!\n");
    }
    else
    {
    System.out.println("\nInvalid Choice!\n");
    }
    }catch(Exception e){}
    }while(vChoice!=4);
    }
    }

  9. #9
    rexson98 is offline Member
    Join Date
    Oct 2008
    Location
    Philippines
    Posts
    8
    Rep Power
    0

    Default

    this is my code,.. ive done it! thnx for your help guys!

  10. #10
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    A lot of redundant code, that's not good. But if it works, at least you're on your way. :) Keep working on it and you'll not only write code that works, but code that's optimized.

  11. #11
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Instead of BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))

    It is better if you use Scanner scan = new Scanner( System.in);

    and get the Vchoice and num with the scan.nextInt()

Similar Threads

  1. JAVA Fast Track Course
    By fortius_computers in forum Reviews / Advertising
    Replies: 0
    Last Post: 08-30-2008, 09:55 AM
  2. Girl from Sweden needs help fast!!! :):)
    By charlotte in forum New To Java
    Replies: 0
    Last Post: 05-25-2008, 03:51 PM
  3. Fast Data Transfer 0.9.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-24-2008, 06:41 PM
  4. Fast Data Transfer 0.8.5
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-12-2007, 06:11 PM
  5. Fast Data Transfer 0.6.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-20-2007, 08:55 AM

Posting Permissions

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