Results 1 to 11 of 11
Thread: pls help me fast!
- 10-07-2008, 06:21 PM #1
Member
- Join Date
- Oct 2008
- Location
- Philippines
- Posts
- 8
- Rep Power
- 0
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!
- 10-07-2008, 07:02 PM #2
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.
- 10-07-2008, 07:03 PM #3
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
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.
- 10-07-2008, 07:50 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
And what about the others? We're not going to do them for you.
- 10-07-2008, 11:56 PM #5
Member
- Join Date
- Oct 2008
- Location
- Philippines
- Posts
- 8
- Rep Power
- 0
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...
- 10-08-2008, 12:43 AM #6
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
- 10-08-2008, 02:49 AM #7
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
- 10-08-2008, 02:28 PM #8
Member
- Join Date
- Oct 2008
- Location
- Philippines
- Posts
- 8
- Rep Power
- 0
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);
}
}
- 10-08-2008, 02:28 PM #9
Member
- Join Date
- Oct 2008
- Location
- Philippines
- Posts
- 8
- Rep Power
- 0
this is my code,.. ive done it! thnx for your help guys!
- 10-08-2008, 05:24 PM #10
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
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.
- 10-15-2008, 12:20 PM #11
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
JAVA Fast Track Course
By fortius_computers in forum Reviews / AdvertisingReplies: 0Last Post: 08-30-2008, 09:55 AM -
Girl from Sweden needs help fast!!! :):)
By charlotte in forum New To JavaReplies: 0Last Post: 05-25-2008, 03:51 PM -
Fast Data Transfer 0.9.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-24-2008, 06:41 PM -
Fast Data Transfer 0.8.5
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-12-2007, 06:11 PM -
Fast Data Transfer 0.6.4
By levent in forum Java SoftwareReplies: 0Last Post: 05-20-2007, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks