i have to do a pizza game ... i wrote all the code already .. it seems to work right most of it . the thing is that my numbers don't came out as they suppose to ... i know my error is in when i calculate the the cheesy crust.... can someone please help!
If the customer chooses to add cheesy crust (to anything but thin & crispy), then add the following to the basic price of the pizza:
k = ec
where:
k = total cost of cheesy crust (dollars)
e = size of pizza crust (inches)
c = cost of materials (dollars per square inch - 0.02)
The size of the pizza crust is a measure of the pizza border:
for round pizzas: e = 2π(d/2) (the circumference)
for rectangular pizzas: e = 2(L+w) (the perimeter)
where
e = size of pizza crust (inches)
d = diameter of round pizza (inches)
L = length of rectangular pizza (inches)
w = width of rectangular pizza (inches)
enter code here
import java.util.Scanner;
public class Pizza {
/*
* calculates price including tax
* #return double
*/
public static double calculatePriceWithTax(double price){
return price * (1 + 0.07);
}
/*
* calculates delivery fee if any
* #return double
*/
public static double deliveryFee(double price){
if(price < 10){
return 3;
}
else if(price >= 10 && price <= 20){
return 2;
}
else if(price >= 20 && price <= 30){
return 1;
}
else{
return 0;
}
}
/*
* calculates basic pricing for total number of pizza
* #return double
*/
public static double calculateCost(int shape, int diameter, int length, int width, int numToppings, int typeDough){
double a = area(shape, diameter, length, width);
double v = volume(a, typeDough);
double cost = a * (0.036 + numToppings * 0.025 ) + v * 0.019;
return cost;
}
/*
* calculates area of a pizza
* #return double
*/
public static double area(int shape, int diameter, int length, int width){
double a = 0.0;
if(shape == 1){ // round pizza
a = Math.PI * (diameter / 2) * (diameter / 2);
}
else if(shape == 2){ // rectangular pizza
a = length * width;
}
return a;
}
/*
* calculates volume of a pizza
* #return double
*/
public static double volume(double a, int typeDough){
double height = 0.0;
switch(typeDough){
case 1:
height = 0.1;
break;
case 2:
height = 0.25;
break;
case 3:
height = 0.5;
break;
case 4:
height = 0.9;
break;
}
return a * height;
}
/*
* calculates cost for cheesy crust
* #return double
*/
public static double calculateChessyCrustCost(int shape, int diameter, int length, int width){
return size(shape, diameter, length, width) * 0.02;
}
/*
* calculates size of pizza
* #return double
*/
public static double size(int shape, int diameter, int length, int width){
if(shape == 1){ // round pizza
return 2 * Math.PI * (diameter / 2);
}
else{ // rectangular pizza
return 2 * (length + width);
}
}
public static void main(String[] args) {
int shape; // 1 => round, 2 => rectangular
int length = 0;
int width = 0;
int diameter = 0;
int numToppings = 0; // number of toppings
int typeDough; // 1 => thin & crusty, 2 => hand tossed, 3 => pan, 4 => texas toast
boolean cheesyCrust = false; // true => add, false => don't add
int lengthCrust = 0;
int numPizza; // number of pizzas ordered
int orderType; // 1 => delivery, 2 => take-out
Scanner scanner = new Scanner(System.in);
System.out.println("This program helps you to order pizza based on your personal preferences.");
System.out.println("It notes your choices and calculates total cost for you, including tax and even delivery fee if applicable.");
System.out.println("Please fill out information for the following:");
System.out.println();
System.out.println("Pizza style:");
System.out.println("1. Round Pizza");
System.out.println("2. Rectangular Pizza");
shape = scanner.nextInt();
if(shape == 1){
System.out.println("Diameter:(inches)");
diameter = scanner.nextInt();
}
else if(shape == 2){
System.out.println("Length:(inches)");
length = scanner.nextInt();
System.out.println("Width:(inches)");
width = scanner.nextInt();}
else {throw new IllegalArgumentException("Enter enter 1 fo Round pizza or 2 for rectangualar pizza");
}
System.out.println("Number of toppings:");
numToppings = scanner.nextInt();
System.out.println("Type of dough:");
System.out.println("1. Thin & Crusty");
System.out.println("2. Classic Hand Tossed");
System.out.println("3. Pan");
System.out.println("4. Texas Toast");
typeDough = scanner.nextInt();
if(typeDough != 1){
System.out.println("Do you want to add cheest crust?[true/false]");
cheesyCrust = scanner.nextBoolean();
}
System.out.println("How many pizzas do you want to order?");
numPizza = scanner.nextInt();
System.out.println("Select your receival method:");
System.out.println("1. Delivery");
System.out.println("2. Take away");
orderType = scanner.nextInt();
double a = area(shape, diameter, length, width);
double v = volume(a, typeDough);
double baseCost = calculateCost(shape, diameter, length, width, numToppings, typeDough);
double crustCost = 0.0;
if(cheesyCrust == true){
crustCost = calculateChessyCrustCost(shape, diameter, lengthCrust, width);
}
double costOne = calculatePriceWithTax(baseCost + crustCost);
double deliveryCharge = 0.0;
if(orderType == 1){ // delivery
costOne += deliveryFee(costOne);
}
double totalCost = deliveryFee(costOne * numPizza) + (costOne * numPizza);
System.out.print("Area: ");
System.out.printf("%.2f", a);
System.out.print(" (inches square)");
System.out.println();
System.out.print("Volume:");
System.out.printf("%.2f", v);
System.out.print(" (cubic inches)");
System.out.println();
System.out.print("Base Cost: ");
System.out.printf("%.2f", baseCost);
System.out.print(" dollar");
System.out.println();
System.out.print("Cost for one pizza: ");
System.out.printf("%.2f", costOne);
System.out.print(" dollar");
System.out.println();
System.out.print("Your order has been processed. Total cost including taxes: ");
System.out.printf("%.2f", totalCost );
System.out.print(" dollars");
System.out.println();
System.out.println("Thank you for using our service.");
}
}
Problem 1 is in your area method:
a = Math.PI * (diameter / 2) * (diameter / 2);
in java, int / int = int, so you have to cast it to double.
to fix this, try the following:
a = Math.PI * (diameter / 2.0d) * (diameter / 2.0d);
The same problem is in your size method. It should look like this:
return 2 * Math.PI * (diameter / 2.0d);
Related
I am having a problem I have not been able to solve searching the internet, basically I need the user to input first the diameter of a sphere then get back the radius and from there use the formula (4 over 3 multiplied by pi (3.14) multiplied by radius (calculated from diameter - user inputted) to the power of 3.
and the other one is very similar..... 4 multiplies by pi (3.14) multiplied by radius to the power of 2.
Now the thing is every time I try to compile I get errors that the method is lossy double not int or the symbol is not recognized.
guys any help would be helpful since I cant find a solution for this.
the code is below :
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter {
public class Pi{
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
int diameter =scan.nextInt();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
int radius = scan.nextInt();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = Diameter / 2;
SecondStep = 4 / 3 * 3.14 * (int) FirstStep * 3;
ThirdStep = 4 * 3.14 * (int) Radius * 2;
Diameter = diameter;
Radius = radius;
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
This is the older source code I was using … which also did not work.
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter
{
public class Pi{
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
double FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
double diameter =scan.nextDouble();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
double radius =scan.nextDouble();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = diameter / 2;
SecondStep = 4 / 3 * Math.PI* Math.pow (FirstStep, 3);
ThirdStep = 4 * Math.PI * radius * Math.pow (2);
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
try out this one
import java.util.Scanner;
public class RadiusConverter {
public class Pi {
}
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int firstStep = 0, diameter = 0, radius = 0, secondStep = 0, thirdStep = 0;
System.out.print("Please enter the diameter of the Sphere: ");
diameter = scan.nextInt();
System.out.print("Please enter the radius of the Sphere: ");
radius = scan.nextInt();
firstStep = diameter / 2;
secondStep = (int) (4 / 3 * 3.14 * firstStep * 3);
thirdStep = (int) (4 * 3.14 * radius * 2);
System.out.println("Your Shpere has a radius of: " + firstStep);
System.out.println("The Volume of your Sphere is: " + secondStep);
System.out.println("The Surface Area of your Sphere is: " + thirdStep);
}
}
}
Output
Please enter the diameter of the Sphere: 250
Please enter the radius of the Sphere: 25
Your Shpere has a radius of: 125
The Volume of your Sphere is: 1177
The Surface Area of your Sphere is: 628
I am new to Java. I am trying to calculate the area of a triangle using the formula:
s = (side 1 + side 2 + side 3)/2
area = square root (side (side - side 1)(side - side2)(side - side3).
If the user enter the three point as:
1.5 -3.4 4.6 5 9.5 -3.4 then the area of the triangle should be 33.6. However, my program runs, but it's giving me an incorrect answer. Below is my code.
// Import Java Scanner
import java.util.Scanner;
import java.lang.Math;
public class Ex_2_19 {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
float side = 0;
float area1 = 0;
float area2 = 0;
float area3 = 0;
float area4 = 0;
float calculatedarea = 0;
//Prompt the user to enter three points of a triangle
System.out.println("Enter point x1:");
System.out.println("Enter point y1:");
System.out.println("Enter point x2:");
System.out.println("Enter point y2:");
System.out.println("Enter point x3:");
System.out.println("Enter point y3:");
//Define the variables
float Pointx1 = input.nextFloat();
float Pointy1 = input.nextFloat();
float Pointx2 = input.nextFloat();
float Pointy2 = input.nextFloat();
float Pointx3 = input.nextFloat();
float Pointy3 = input.nextFloat();
//Formula to calculate the area of a triangle
side = (Pointx1 + Pointy1 + Pointx2 + Pointy2 + Pointx3 + Pointy3) / 2;
area1 = side - (Pointx1 + Pointy1);
area2 = side - (Pointx2 + Pointy2);
area3 = side - (Pointx3 + Pointy3);
area4 = side * area1 * (area2) * (area3);
calculatedarea = (float) (Math.sqrt(area4));
//calculatedarea = (float) (Math.sqrt(area1)*(area2) * (area3));
//Print result
System.out.println("The area of the triangle is " + calculatedarea);
}
}
You are trying Heron's Formula - note that a, b, c are the euclidean distance between the points, thus will need to be computed by sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)), etc., not just the sum of differences.
import java.util.Scanner;
public class clockwise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the coordinates in a clowise order");
System.out.println("Enter the GPS coordinates for the 1st city: ");
double coordinateX1 = input.nextDouble();
double coordinateY1 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 2nd city: ");
double coordinateX2 = input.nextDouble();
double coordinateY2 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 3rd city: ");
double coordinateX3 = input.nextDouble();
double coordinateY3 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 4th city: ");
double coordinateX4 = input.nextDouble();
double coordinateY4 = input.nextDouble();
//
double earthRadius = 6371.01;
// Get distance
// distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos( y1−y2))
// ****************************************************************************1
// 1 35.2270869 -80.8431267
double distance1 = (earthRadius)
* Math.acos(Math.sin(coordinateX1) * Math.sin(coordinateX2))
+ Math.cos(coordinateX1) * Math.cos(coordinateX2)
* Math.cos(coordinateY1 - coordinateY2);
System.out.println("distance1: "+distance1);
// 2 35.2270869 -80.8431267
double distance2 =
(earthRadius)
* Math.acos(Math.sin(coordinateX2) * Math.sin(coordinateX4))
+ Math.cos(coordinateX2) * Math.cos(coordinateX4)
* Math.cos(coordinateY2 - coordinateY4);
System.out.println("distance2: "+distance2);
// 3 28.5383355 -81.3792365
double distance3 = (earthRadius)
* Math.acos(Math.sin(coordinateX4) * Math.sin(coordinateX1))
+ Math.cos(coordinateX4) * Math.cos(coordinateX1)
* Math.cos(coordinateY4 - coordinateY1);
System.out.println("distance3: "+distance3);
// ******************************************************************************2
// 4 33.7489954 -84.3879824
// 1 35.2270869 -80.8431267
double distance01 = (earthRadius)
* Math.acos(Math.sin(coordinateX2) * Math.sin(coordinateX3))
+ Math.cos(coordinateX2) * Math.cos(coordinateX3)
* Math.cos(coordinateY2 - coordinateY3);
// System.out.println("distance: "+distance01);
// 2 32.0835407 -81.0998342
double distance02 = (earthRadius)
* Math.acos(Math.sin(coordinateX3) * Math.sin(coordinateX4))
+ Math.cos(coordinateX3) * Math.cos(coordinateX4)
* Math.cos(coordinateY3 - coordinateY4);
//System.out.println("distance: "+distance02);
// 3 28.5383355 -81.3792365
double distance03 =
(earthRadius)
* Math.acos(Math.sin(coordinateX4) * Math.sin(coordinateX2))
+ Math.cos(coordinateX4) * Math.cos(coordinateX2)
* Math.cos(coordinateY4 - coordinateY2);
// System.out.println("distance: "+distance03);
double rodistance1 = Math.ceil(distance1);
double rodistance2 = Math.ceil(distance1);
double rodistance3 = Math.ceil(distance3);
double rodistance01 = Math.ceil(distance01);
double rodistance02 = Math.ceil(distance02);
double rodistance03 = Math.ceil(distance03);
double s1 = (rodistance1 + rodistance2 + rodistance3) / 2;
double s2 = (rodistance01 + rodistance02 + rodistance03) / 2;
double area1 = Math.sqrt(s1 * (s1 - rodistance1) * (s1 - rodistance2)
* (s1 - rodistance3));
double area2 = Math.sqrt(s2 * (s2 - rodistance01) * (s2 - rodistance02)
* (s2 - rodistance03));
double totalArea = Math.ceil(area1 + area2);
System.out.println("The area is: " + totalArea);
}
}
//SAMPLE
//Please enter the coordinates in a clockwise order.
//Enter the GPS coordinates for the 1st city: 35.2270869 -80.8431267
//Enter the GPS coordinates for the 2nd city: 32.0835407 -81.0998342
//Enter the GPS coordinates for the 3rd city: 28.5383355 -81.3792365
//Enter the GPS coordinates for the 4th city: 33.7489954 -84.3879824
//The area is: 117863.342
I am getting 1.06664794E8 What can I do to get the answer as the sample? Unless I am having errors with my formulas, I should be getting a ok answer. I am using Math.ceil() it might not be what I really wanted I needed to round to 3 decimals .................................................................
Use BigDecimal and setScale()
BigDecimal bg1 = new BigDecimal("1.06664794E8");
// set scale of bg1 to 3 and using CEILING as rounding mode
bg1 = bg1.setScale(3, RoundingMode.CEILING);
System.out.println("After changing the scale to 3 and rounding is "+bg1);
Output:After changing the scale to 3 and rounding is 106664794.000
You can use BigDecimal here
double val = 1.06664794E8;
BigDecimal bigDecimal = new BigDecimal(val);
System.out.println(bigDecimal);
Out put:
106664794
Got scolded earlier for not specify the question. Promise to do better this time.
For some reason, the loop doesn't stop when i enter 0, instead it continues to print out the main method again. How do i fix this? I couldn't find out what is wrong with it, since everything else runs smoothly.
Also, I want to use Switch instead of If, but i keep getting Duplicate Variable error. Why?
import java.util.Scanner;
public class FindArea
{
// side is only input provided to cube. Area of the cube is returned
public static double cube (double side)
{
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere (double radius)
{
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder (double radius, double height)
{
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut (double outerR, double innerR)
{
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
do
{
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
} while (n != 0);
System.exit(1);
}
}
It prints the output one more time because the comparison is not made until the end of the Do-While loop.
It checks for all cases of n other than 0, and prints the output no matter what.
I think what you're looking for is more so:
n = input.nextInt();
if(n!=0){
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
}
note the n!=0
Additionally look into the use of switch-case, once you learn them these comparisons will not only be faster, but very easy to comprehend
You can try this with your switch case -
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
else{
switch(n){
case 0:{
System.out.println("You enter 0 to exit");
break;
}
case 1:{
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
}
case 2:{
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
break;
}
case 3: {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
}
case 4:{
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
}
}
}
}
i have done this using switch case. Very simple as well
public class FindArea {
// side is only input provided to cube. Area of the cube is returned
public static double cube(double side) {
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere(double radius) {
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder(double radius, double height) {
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut(double outerR, double innerR) {
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");
int n = 0;
double side = 0;
double Area = 0;
double radius = 0;
double height = 0;
n = input.nextInt();
switch (n) {
case 0:
System.out.println("Exit");
System.exit(1);
break;
case 1:
System.out.print("Enter side measurement of cube: ");
side = input.nextDouble();
Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
case 2:
System.out.print("Enter radius measurement of sphere: ");
radius = input.nextDouble();
Area = sphere(radius);
System.out.println("The area of the sphere is: " + Area);
break;
case 3:
System.out.print("Enter radius measurement of cylinder: ");
radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
height = input.nextDouble();
Area = cylinder(radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
case 4:
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
Area = doughnut(outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
default:
System.out.println("--------");
System.out.println("Invalid option selected");
System.out.println("--------");
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
break;
}
}
}
Good Luck.
I am new to Java and still trying to learn my way around it. Today my teacher gave an assignment where we make a BMI calculator in Java. One of the steps is to make it show the BMI categories. So the user would be able to look at it and see where they stand. I got everything else done but am running into an issue.
Here is the script:
import java.util.Scanner;
public class BMI {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double weight = 0.0;
double height = 0.0;
double bmi = 0.0;
System.out.print("Enter your weight in pounds: ");
weight = input.nextInt();
System.out.print("Enter your height: ");
height = input.nextInt();
bmi = ((weight * 703)/(height * height));
System.out.printf("Your BMI is", bmi);
System.out.println("BMI VALUES");
System.out.println("Underweight: Under 18.5");
System.out.println("Normal: 18.5-24.9 ");
System.out.println("Overweight: 25-29.9");
System.out.println("Obese: 30 or over");
}
}
Here is the result:
Your BMI isBMI VALUES
Underweight: Under 18.5
Normal: 18.5-24.9
Overweight: 25-29.9
Obese: 30 or over
What am I doing wrong and how do I fix this?
You forgot the placeholder for the bmi (and the line terminator):
System.out.printf("Your BMI is %f\n", bmi);
Here's the formatting syntax guide for your reference.
Try this:
System.out.printf("Your BMI is %f\n", bmi);
You can also print like this:
System.out.println("Your BMI is " + bmi);
While you are using printf, the following also works:
System.out.println("Your BMI is " + bmi);
Ideally you should print floats / doubles in a readable format Normally BMI value is measured up to 2 decimal place
System.out.format("Your BMI is %.2f%n",bmi);
You can use arrays like this:
public static void main(String[] args) {
double[] ht = new double[5];
double[] wt = new double[5];
double[] bmi = new double[5];
String[] sht = new String[5];
String[] swt = new String[5];
for (int i = 0; i < ht.length; i++) {
sht[i] = JOptionPane.showInputDialog("Enter Hight");
swt[i] = JOptionPane.showInputDialog("Enter Weight");
ht[i]=Double.parseDouble(sht[i]);
wt[i]=Double.parseDouble(swt[i]);
bmi[i]=wt[i]/(ht[i]*ht[i]);
System.out.println("BMI IS "+bmi[i]+"Kg/m2");
JOptionPane.showMessageDialog(null,"BMI IS "+bmi[i] +"Kg/m2");
if(bmi[i]>25){
JOptionPane.showMessageDialog(null,"YOU ARE AT RISK");
System.out.println("YOU ARE AT RISK");
}else {
JOptionPane.showMessageDialog(null,"YOU ARE Healthy");
System.out.println("YOU ARE Healthy ");
}
}
}
It's probably more coding but you could use JOptionPane
input = JOptionPane.showInputDialog("What is you weight in pounds?");
weight = Double.parseDouble(input);
input = JOptionPane.showInputDialog("What is your hight is inches?");
height = Double.parseDouble(input);
mismatch between variable type declaration (double) and your declaration of user input type (nextInt)
I would use nextDouble as an input type if you want to maintain variable type as a double.
https://github.com/arghadasofficial/BMI/blob/master/src/argha/bmi/Bmi.java
public class Bmi {
//Variables
private double result; //Stores the result
private String bmi; //Stores the Bmi result in String
private String status; //Stores the status (Normal......)
private static DecimalFormat decimalFormat = new DecimalFormat(".#"); //Formating the result
/**
* Get the BMI from calculation and returns the result in String format
*
* #return BMI
*/
public String getBmi() {
/**
* Following code example : bmi = decimalFormat.format(result);
*
* if bmi is 23.12345 then remove 2345 and return 23.1 using DecimalFormat class provided by Java
*
*/
bmi = decimalFormat.format(result);
return bmi;
}
/**
* Get the status according to the BMI result and returns the Status in
* String format
*
* #return Status
*/
public String getStatus() {
return status;
}
/**
* Get weight and height in double format and do the calculation for you
*
* #param weight - accepts weight in kilograms
* #param height - accept height in centimeters
*/
public void calculateBmi(double weight, double height) {
/**
* First get weight and height then convert height to meters then
* multiply the height itself to height and then divide the height with
* weight then set the actual value to result variable
*/
//Convert the height from cm to m
double meters = height / 100;
//Multiply height by height
double multiply = meters * meters;
//divide the height with weight in kg
double division = weight / multiply;
//set the value to result variable
result = division;
//call checkStatus method for checking Status
checkStatus();
}
/**
* Private method for checking bmi and returns the status It remains private
* because we don't need to access this from somewhere else.
*/
private void checkStatus() {
/**
* Check :
*
* if BMI is less than 18.5 then set status to Underweight if BMI is
* greater than 18.5 and less than 25 then set status to Normal if BMI
* is greater than 25 and less than 30 then set status to Overweight if
* BMI equals to 30 or greater than 30 then set status to Obese
*/
if (result < 18.5) {
status = "Underweight";
} else if (result > 18.5 && result < 25) {
status = "Normal";
} else if (result > 25 && result < 30) {
status = "Overweight";
} else if (result == 30 || result > 30) {
status = "Obese";
}
}
}
A: I am not sure that bmi = ((weight * 703)/(height * height)); is right.
I came across a formula which might work for you
BMI = ((weight/(height * height))* 10000;
B: System.out.printf(“Your BMI is”, bmi); is not needed
System.out.prinln( “ Your BMI is “ + BMI);
I hope it work for you
best of luck keep it up