-
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.
-
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.
-
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?
-
Reply for the Code
Here is what ive done so far
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class triangles {
public static void main(String[] args )throws IOException {
InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader buffer = new BufferedReader(reader);
int area;
int base;
int altitude;
int triangle;
System.out.print("Enter base of triangle:\n");
base = Integer.valueOf(buffer.readLine()).intValue();
System.out.print("Enter number triangle:\n");
triangle = Integer.valueOf(buffer.readLine()).intValue();
System.out.print("Enter altitude of triangle:\n");
altitude = Integer.valueOf(buffer.readLine()).intValue();
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
-
What kind of output are you getting?
Are you receiving any errors?
-
 Originally Posted by DevMcg123
Here is what ive done so far
Code:
System.out.print("Enter base of triangle:\n");
base = Integer.valueOf(buffer.readLine()).intValue();
System.out.print("Enter number triangle:\n");
triangle = Integer.valueOf(buffer.readLine()).intValue();
System.out.print("Enter altitude of triangle:\n");
altitude = Integer.valueOf(buffer.readLine()).intValue();
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
-
 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.
-
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)
Similar Threads
-
Replies: 27
Last Post: 02-11-2016, 05:29 AM
-
Replies: 1
Last Post: 08-12-2007, 09:38 PM
-
By westweb in forum VB Classic
Replies: 8
Last Post: 06-11-2007, 03:17 PM
-
Replies: 1
Last Post: 02-18-2007, 07:43 PM
-
By Sinni in forum VB Classic
Replies: 1
Last Post: 11-26-2002, 12:56 PM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|