Results 1 to 10 of 10
Thread: Triangles
- 10-12-2008, 11:07 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Triangles
Been working on this awhile and any help would be appreciated..Need to make 3 triangles using Scanner class(This part isn't part of my code yet, been concentrating on getting triangles right)
Triangle shapes are:
1
22
333
4444
55555
1
222
33333
4444444
555555555
555555555
4444444
33333
222
1
Here's what I have so far. The last 2 Triangles are actually pascal triangles, why it's coming out in the thread like it is, I don't know...
Java Code:class TriangleTest1 { public static void main(String[] args) { int counter = 1; int toPrintT1 = 1; int toPrintT2 = 1; int toPrintT2A = 1; while (toPrintT1 < 6) { for ( int i = 0; i < toPrintT1; i++) { System.out.print(toPrintT1); } System.out.println(); toPrintT1++; } System.out.println(); while (toPrintT2 < 6) { for (int j = 0; j < toPrintT2; j++) { System.out.print(toPrintT2); } if ( toPrintT2!= 1) { for (int k = 0; k < toPrintT2A - 1; k++) { System.out.print(toPrintT2A); } } System.out.println(); toPrintT2A++; toPrintT2++; } } }Last edited by CodeDog; 10-12-2008 at 11:12 PM.
- 10-12-2008, 11:54 PM #2
I missed your question. Can you show what the program outputs now and what you want it to be?
- 10-13-2008, 03:01 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Output is:
1
22
333
4444
55555
1
22 2
333 33
4444 444
55555 5555
First one is correct...
Second one should be a pascal triangle...Not an extension of the first one.
Haven't worked on third triangle yet, but it's to be an upside down pascal triangle.
Would using scanner class be coded in this one or in the class Triangle?
- 10-13-2008, 05:28 AM #4
Padding with blanks
For the Pascal triangles (Note for Forum: Pascal triangles are pirmide shaped), you need to pad the output with blanks...
- For the upright pascal triangle, the number of blanks are per line decrease as the triangle grows
- For the upside down pascal triangle, the number of blanks increases as the triangle decreases
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 10-13-2008, 08:11 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Not sure I am following you..Could you give an example?
- 10-14-2008, 01:17 AM #6
padding with blanks
I was thinking something like (pseudo code):
System.out.println(" " + pascalPiramideNumber);//4 blanks before number
System.out.println(" " + pascalPiramideNumber);//3 blanks before 3 numbers
System.out.println(" " + pascalPiramideNumber);//2 blanks before 5 numbers
System.out.println(" " + pascalPiramideNumber);//1 blanks before 7 numbers
System.out.println("" + pascalPiramideNumber);//0 blanks before 9 numbers
etc..
- To build it, you would have to decrement the blank spaces and increment the rows of numbers through a loop.
- Just do the reverse with the inverted Pascal piramide.
Hope this helps.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 10-14-2008, 02:56 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
OK great thanks for your help, it's really appreciated.
- 10-14-2008, 01:02 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Java Code:#include <iostream.h> void rectangle(int heigth, int width); void isosceles(int heigth); void diamond(int heigth); int main() { int heigth; int width; char c; cout<<"please choose the shapes that you would like using,if you want to quit please enter Q(uit)"<<endl; cout<<"(R/I/D)"<<endl; cin>>c; if(c == 'R' || c == 'r') { cin>>heigth>>width; rectangle(heigth, width); } if(c=='I' || c=='i') { cin>>heigth; isosceles(heigth); } if(c=='D' || c=='d') { cout<<"Please enter an odd number!"<<endl; cin>>heigth; if((heigth%2) != 0) { diamond(heigth); } else { cout<<"This is an IllegalAgrument!"<<endl; } } return 0; } void rectangle(int heigth, int width) { int i = 0; int j = 0; char c = '*'; for(i=0; i<heigth; i++) { for(j=0; j<width; j++) { cout<<c; } cout<<endl; } } void isosceles(int heigth) { int i; int j = 0; int k = 0; int row = heigth; int spacenum; int starnum; for(k=0; k<heigth; k++) { spacenum = row--; if(k==0) { starnum = 1; } else { starnum = k*2+1; } for(i=spacenum; i>0; i--) { cout<<" "; } for(j=1; j<=starnum; j++) { cout<<"*"; } cout<<endl; } } void diamond(int heigth) { int rownum = heigth; int spacenum; int starnum; int i=0; int j=1; int k; int a = rownum/2; for(i=0; i<heigth; i++) { if(i<rownum/2) { spacenum = a--; } else { spacenum = a++; } //cout<<spacenum<<"sapcenum"<<endl; starnum = rownum -spacenum*2; //cout<<starnum<<"starnum"<<endl; for(j=0; j<spacenum; j++) { cout<<" "; } for(k=0; k<starnum; k++) { cout<<"*"; } cout<<endl; } }
- 10-14-2008, 02:04 PM #9
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Java Code:public class Number { public static void main(String []s) { num_array n=new num_array(3,2,false) ; n.show() ; } } class num_array { private int a ;//最多输出到几 private boolean order ;//正序还是倒序 private int distance ;//表示出的时候的每一行之间数字个数之差 这个数必须大于或者等于0 public num_array(int a,int distance,boolean order)//true表示正序 false表示倒序 { this.a=a ; this.order=order ; this.distance=distance ; } public num_array()//默认的是最高输出到5,正序,每一行差一个数字 { this.a=5 ; this.order=true ; this.distance=3 ; } public void show() { if(order==true) { if(distance==0) for(int i=1;i<=a;i++) System.out.println(i) ; if(distance>0) { for(int i=1;i<=a;i++) { for(int j=1;j<=(i-1)*distance+1;j++) System.out.print(i) ; System.out.println() ; } } } if(order==false) { if(distance==0) for(int i=a;i>=1;i--) System.out.println(i) ; if(distance>0) { for(int i=a;i>=1;i--) { for(int j=(i-1)*distance+1;j>=1;j--) System.out.print(i) ; System.out.println() ; } } } } }
- 10-14-2008, 09:18 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks