1,105,447 Community Members

NetBeans Java -how to reverse the triangle-

Member Avatar
sk8ergirl
Junior Poster
123 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm practicing for my java test on sunday and I did this lab and everything is okay except for the zero I don't know why the zero is there and I want to reverse the triangle form this :
0
10
210
3210
43210

to this
1
21
321
4321

and form
0
10
210
3210
43210
to
1
12
123
1234

and from
0
10
210
3210
43210
to
1234
123
12
1
and form
0
10
210
3210
43210
to
4321
432
43
4

and form
0
10
210
3210
43210
to
space 1
space23
space234


how can I do it ??
note: I can only for loop
and this is my code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trianglenumber;

import java.util.Scanner;

public class TriangleNumber {

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("What is size of triangle ");
        int count = input.nextInt();
        for (int i = 0; i <= count; i++) {
            System.out.println(spaceLine(count + i) + numberLine(i) + "" + sum(i));
        }

    }

    public static String numberLine(int count) {
        String result = "";
        for (int i = count; i > 0; i--) {
            result = result + i;
        }
        return result;
    }

    public static String spaceLine(int count) {
        String result = "";
        for (int i = count; i > 0; i--) {
            result = result + "";
        }
        return result;
    }

    public static int sum(int count) {
        int result = 0;
        for (int i = 0; i > count; i++) {
            result = result + i;
        }
        return result;
    }
}
Member Avatar
stultuske
Posting Expert
5,332 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

I assume this is because in your for loops, you always start with 0.
try by starting them with 1

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: