-1

I have a huge problem. I take Informatic class and we got a job to make a program that I have no idea how to make. Can you please help me?

Make a program in Java that will write this triangle in * if you put in an optional height that is written in the variable. The example if the number 4 is written looks like this:

      *
    * *
  * * *
* * * *

And if you have some more time can you please explain to me why did you do it like this? And how would you make this on paper?

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • The point is that i really have no idea how to do it. we've been learning different things than this, so that's why it's so hard to me. – user3180629 Jan 11 '14 at 09:22
  • 3
    Google is [**your friend**](https://www.google.com/search?q=how%20to%20print%20screen#q=how%20to%20print%20a%20triangle%20in%20java) – Paul Samsotha Jan 11 '14 at 09:23
  • Go through some good java tutorials. Learn about for loops and printing a character in java (In your case *). Try some code (may be the most weirdest looking) and then I would be glad to help you. – Crickcoder Jan 11 '14 at 09:26
  • Try to think about what is the relation between the current height, the number of spaces and the number of stars to be printed. When you found this, that should be trivial. – Alexis C. Jan 11 '14 at 09:44

1 Answers1

0

I thought it might be fun to do it in one line:

public static void printTriangle(int size) {
    for (int i = 0; i < size; i++) System.out.println(Integer.toString(1 << (size - 1 - i), 2).replaceAll("^.", "").replaceAll(".", "  ") + Integer.toString(1 << i, 2).replaceAll(".", "* "));
}

Using bit-shifting kung fu and Integer.toString() with base 2 to produce a string of the required length.


Test code:

printTriangle(6);

Output:

          * 
        * * 
      * * * 
    * * * * 
  * * * * * 
* * * * * * 
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • @user3180629 What will you say when your instructor asks you how this works? Are you sure Bohemian has helped you? – Dawood ibn Kareem Jan 11 '14 at 10:25
  • He probably won't ask. we just have to send the solved thing to him. And i'm in big trouble. – user3180629 Jan 11 '14 at 10:29
  • I used to teach this stuff for a living. I ALWAYS asked for an explanation, if a student handed in an unnecessarily complex answer. – Dawood ibn Kareem Jan 11 '14 at 10:44
  • i don't want to argue with you, but he probably won't ask, really. – user3180629 Jan 11 '14 at 10:48
  • 1
    Well, so long as you've learnt something today, Bohemian's effort hasn't been a waste of time. One day, there'll be ONE MORE programming graduate who doesn't know how to program. – Dawood ibn Kareem Jan 11 '14 at 10:50
  • yes, i know. i've tried to do like he said it would work and it just has 4 mistakes which i will solve somehow:) – user3180629 Jan 11 '14 at 10:52
  • don't be rude, it's just homework. besides i won't gradute from pragrammating or something.. – user3180629 Jan 11 '14 at 10:53
  • 1
    @user3180629: because you couldn't conceive how to even draw triangles you most likely will get a **ZERO** if not referred to the dean for plagiarism. Your instructor won't even have to ask; it will be *obvious* you didn't write it. There's no shortcut to learning and if you're taking this course and don't care about learning anything then you're wasting at least two people's time. – ChiefTwoPencils Jan 11 '14 at 17:41
  • 1
    @BobbyDigital is completely right. I posted this for fun (and to show off), but this is not meant to be submitted - it could only have been written by someone quite experienced. Even a novice level 20 line implementation would be instructive for you to write. Work it out, be methodical and break the problem down into smaller pieces. Solve each piece then put it together. – Bohemian Jan 11 '14 at 20:51