Height Guestimate

Trouble with result

Page 1 of 1

6 Replies - 1455 Views - Last Post: 18 March 2010 - 09:03 AM Rate Topic: -----

#1 Guest_hambone907*


Reputation:

Height Guestimate

Posted 18 March 2010 - 03:48 AM

So I have successfully made a program that can estimate the height of a child from given input heights from a mother and father. However, I have to display the output in inches, and display in ft and inches. The ft and inches is making it difficult because my result formula as of now gives out a decimal place for ft. For example, 64 inches could be the inches result, and then it would say " 64 inches which is = to 5.3333333ft 4inches. It would be correct if the .3333 inches werent there, but I dont know how to take it away. If anyone could help it would be greatly appreciated. My code is as follows...


//*The book was unspecific on whether to use a double or int format, so I used an int format because it's more practicle.*

import java.util.Scanner;
import java.text.DecimalFormat;
public class Height
{
   public static void main( String args[] )
   {
      Scanner input = new Scanner( System.in );

         double x;
         double y;
         double result;
         char z;


      System.out.println("Enter Height of Mother in inches");

      x = input.nextDouble();

      System.out.println("Enter Height of Father in inches");

      y= input.nextDouble();

	  System.out.println("	Indicate Gender of Child by Typing M for male or F for Female ");

      z= input.next().charAt(0);


      if (Character.toLowerCase(z)=='m') {

         result = (((x*(13/12))+y)/2);

			System.out.println(" ");
            System.out.println("The Height of the male child is "+ result + "inches, which is = to " + (result/12) +"ft " +(result%12) +"inches");

	 }
		 else if (Character.toLowerCase(z)=='f'){

		result = (((y*(12/13))+ x)/2);

			System.out.println(" ");
	  		System.out.println("The Height of the female child is " + result + "inces, which is = to " + (result/12) + "ft " + (result%12) +"inches");
}
}
}





Is This A Good Question/Topic? 0

Replies To: Height Guestimate

#2 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: Height Guestimate

Posted 18 March 2010 - 03:54 AM

You could cast the result to an int like this:

(int)(result/12)


or use the Math.round() function to round the result.
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2044
  • View blog
  • Posts: 4,231
  • Joined: 11-December 07

Re: Height Guestimate

Posted 18 March 2010 - 03:57 AM

If you want to round result down you can cast it to an int like this:

System.out.println("The Height of the female child is " + result + "inces, which is = to " + (int)(result/12) + "ft " + (result%12) +"inches");

This post has been edited by cfoley: 18 March 2010 - 03:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_hambone907*


Reputation:

Re: Height Guestimate

Posted 18 March 2010 - 06:30 AM

Thank you all for your help, but now i need to loop the program so it will continue unless the user prompts the program to stop itself with an input, such as entering a certain number to exit. I know I need to insert a do while the program applies, such as (!exit), but im not clear on how to exactly go about doing so.
Was This Post Helpful? 0

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2044
  • View blog
  • Posts: 4,231
  • Joined: 11-December 07

Re: Height Guestimate

Posted 18 March 2010 - 06:50 AM

How about something like this:

boolean playItAgainSam = true;
do {

  ***get input from user
  if (userWants to quit) {
    playItAgainSam = false;
  }

} while(playItAgainSam);

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Height Guestimate

Posted 18 March 2010 - 06:51 AM

You could do something tike this:
while(true) {
    System.out.println("Enter Height of Mother in inches (or -1 to quit)");
    x = input.nextDouble();
    if (x == -1) {
        break;
    }
 
    ...
}


Was This Post Helpful? 1
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2044
  • View blog
  • Posts: 4,231
  • Joined: 11-December 07

Re: Height Guestimate

Posted 18 March 2010 - 09:03 AM

See, I like Dogstopper's method. I think the code is cleaner and it's what I do when I'm programming. However, if you're doing it for class, teachers & lecturers don't like the break command for exiting loops, saying it's bad practice. It often is bad practice but for something like this it's ideal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1