Web Analytics Made Easy -
StatCounter any one help me - CodingForum

Announcement

Collapse
No announcement yet.

any one help me

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • any one help me

    I need some help in java class
    bcoz i'm new in java i know it's easy for many of you but i really need help

    how can i write a code to solve this problem

    *****1
    ****212
    ***32123
    **4321234
    *543212345

    i need code to write this numbers in the same way " but without stars bcoz i couldn't write spaces in this post "
    please help me i need it today
    Last edited by boody2005; Oct 6, 2006, 12:14 AM.

  • #2
    Do you know how to output a string in java?

    If yes:
    Just output 5 strings each with the numbers you want, and use space bar to fix how you output looks(java is not like CodingForum and will let you display spaces).
    That's the easy way, and it's guranteed to be working by the end of the day.
    The other way would be using loops, but I recomend you start of without them.

    If no:
    I'm sure by now you have made a program that outputs "hello java". Use the same code, and use 5 times the System.out.println(). Change "hello java" as appropiate.

    Comment


    • #3
      so how can i make it with loops is it hard

      Comment


      • #4
        Depends, did you already get it working without loops?
        A loop is something that will do the same functions over and over again until a condition is met.

        for example, to start with, you could do a simple loop:
        Code:
        for(int i=1; i<6; i++){
        System.out.println(i);
        }
        
        /*Output:
        1
        2
        3
        4
        5
        */
        Later on you could modify it to do
        1
        21
        321...
        And after that, display the other half
        1
        212
        32123...
        And at the very end get the triangular shape.
        Try to do the example avobe, and ask questions as needed.
        Last edited by daniel_g; Oct 6, 2006, 01:07 AM.

        Comment


        • #5
          I'd be more than happy to help you with this problem but I'm not going to simply do it for you.
          If you can provide your code, then I can point out where you might have gone wrong.

          I tackled this problem in two steps.
          Calculate what the starting number should be for the current line. (keep some counter and add 1 to it each time you finish a line) then,

          1) I used a for loop to print the first have of the line. The number of times this loop executed was equal to whatever line number I was printing. Subtract 1 to the number you're printing after each iteration. i.e 1 iteration of the loop for the first line, 2 for the second and so on up to 5 (of whatever the max line number is).

          2) I used another for loop to print the second have of the line. The number of times this loop executed was equal to whatever line number I was printing minus 1. Add 1 to the number you're printing after each iteration. i.e 1-1 iteration of the loop for the first line, 2-1 for the second and so on up to 5-1 (of whatever the max line number is).

          I haven't been totally clear in my explanation above, but it might give you some hints.

          Comment


          • #6
            In the future, please use a more descriptive subject when posting a question. See posting guidelines.
            OracleGuy

            Comment


            • #7
              thanx all for your answers and i really did it and wrot the code to solve it this is the code



              import javax.swing.JOptionPane;
              public class Traingle

              {


              public static void main ( String[] args) // "this is my first code in java :-)"
              {
              // int max=5 ;
              int space = 0 ;




              int max = Integer.parseInt(JOptionPane.showInputDialog("Enter The Max Number only 1 : 9 ")); // ask for the max number

              for ( int row = 1; row <= max; row++) // row loop
              {

              for ( space = 1; space <= max-row; space++) // spaces loop


              System.out.print (" "); // spaces out


              for ( int leftTri = row ; leftTri >= 2 ;leftTri--) // left traingel loop

              System.out.print (leftTri); // left traingel out

              {

              for ( int rightTri = 1; rightTri <= row; rightTri++ ) // right traingel loop


              System.out.print(rightTri); // right traingel out

              }

              for (int trunc=2; trunc>0; trunc--) // extra line loop
              {


              for ( space = 1; space <= max-1; space++) // spaces loop


              System.out.print (" "); // spaces out
              System.out.println ();
              }
              }
              }
              }

              Comment

              Working...
              X