DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

#### Hybrid View

1. Registered User
Join Date
Nov 2008
Posts
2

## While loop program

Plz help. I am a rookie in Java programming and have a question on how to go by doing a while loop program.

Write a program which reads in the base and altitude of a triangle, computes the area, and prints the base, altitude and area of each in good form. Continue until the user enters a "-1" for the base.

I know the output is suppose to look like this:
Tri base altitude area
1 ## ## ##
2 ## ## ##

Thanks.

2. Registered User
Join Date
Jul 2005
Location
SW MO, USA
Posts
299
Break the project up into small easy steps:
First don't worry about getting the input, just hard code it in variables:
int base = 10;
int altitude = 20;
etc
Then compute the area and print it out

Next write a program that gets those values from the user, one at a time.

3. Welcome to DevX

We are more than happy to help you help yourself. What code do you have so far that doesn't seem to be working for you?

4. Registered User
Join Date
Nov 2008
Posts
2

Here is what ive done so far
Code:
``` import java.io.BufferedReader;
import java.io.IOException;

public class triangles {
public static void main(String[] args )throws IOException {

int area;
int base;
int altitude;
int triangle;

System.out.print("Enter base of triangle:\n");

System.out.print("Enter number triangle:\n");

System.out.print("Enter altitude of triangle:\n");

area = 1/2 * base * altitude;

while(base <= 1){
System.out.println("trangle      base     altitude   area ");
System.out.println("-------      ----     --------   ----");
System.out.println(+ triangle + "" + base + "" + altitude + "" + area);

base = base + 1;

}
}
}```
Last edited by Hack; 11-11-2008 at 07:24 AM. Reason: Fixed Code Tags

5. Registered User
Join Date
Nov 2008
Posts
3
Originally Posted by DevMcg123
Here is what ive done so far
Code:
```System.out.print("Enter base of triangle:\n");

System.out.print("Enter number triangle:\n");

System.out.print("Enter altitude of triangle:\n");
You probably want to put that inside the while loop. Also, you can use a counter for the triangle # instead of asking the user to enter it.

Code:
`area = 1/2 * base * altitude;`
Your area, base, and altitude variables are integers. Are you sure you don't want floats or longs? Also, I'd rewrite that as
Code:
`area = base * altitude / 2`

6. Originally Posted by Clarox
you can use a counter for the triangle # instead of asking the user to enter it.
I'm guessing that rather than 0 he would want a user input starting point.

7. Registered User
Join Date
Nov 2008
Posts
2
its better to use do - while that while.
then your code will look like :
Code:
```do{

if(base == -1 || altitude == -1 ||  height == -1)
break;
// find area
}while(true)```

8. What kind of output are you getting?

Are you receiving any errors?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise