While loop program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: While loop program

Hybrid View

  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Question 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. #2
    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. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  4. #4
    Join Date
    Nov 2008
    Posts
    2

    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 08:24 AM. Reason: Fixed Code Tags

  5. #5
    Join Date
    Nov 2008
    Posts
    3
    Quote Originally Posted by DevMcg123 View Post
    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

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by Clarox View Post
    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.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    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. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What kind of output are you getting?

    Are you receiving any errors?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 05:12 AM
  2. Replies: 1
    Last Post: 08-12-2007, 10:38 PM
  3. Program Watcher
    By westweb in forum VB Classic
    Replies: 8
    Last Post: 06-11-2007, 04:17 PM
  4. help, dont know why program is too slow
    By bob2000 in forum Java
    Replies: 1
    Last Post: 02-18-2007, 08:43 PM
  5. Replies: 1
    Last Post: 11-26-2002, 01: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center