Can someone please help me with this programming problems?

2009-08-03 6:36 am
There are 10 errors or omissions in the source code below. Please find and correct it.



For example, if the user weighs 61.5 kilograms and is 1.64 metres in height, the output
should look as follows:

Enter your weight in Kilograms: 61.5
Enter your height in Metres: 1.64
Your BMI is: 23



// program to calculate Body Mass Index (BMI)
public class CalculateBMI {
public start() {

System.out.println("Enter your weight in Kilograms: ");
int weight = Double.parseDouble(Keyboard.readInput())…
System.out.print(Enter your height in Metres: ");
double height = Integer.parseInt(Keyboard.readInput);
double bodyMassIndex = weight / height * height);
System.out.print("Your BMI is: ")
System.out.println(Math.round(BodyMass…
}

回答 (2)

2009-08-03 7:04 am
✔ 最佳答案
Don't have Java compiler on this computer but I think this should do it. Also, I don't know of a standard Keyboard class in Java, so I assume you are allowed to keep this. Otherwise you would have to instantiate a buffered reader of some sort to get keyboard input.

(Had to put some stuff on multiple lines because Yahoo! answers was cutting it off).


// program to calculate Body Mass Index (BMI)
public class CalculateBMI {
public void start() {

System.out.print("Enter your weight in Kilograms: ");
double weight = Double.parseDouble(
  Keyboard.readInput());
System.out.print("Enter your height in Metres: ");
double height =
  Double.parseDouble(Keyboard.readInput());
double bodyMassIndex = weight / height * height;
System.out.print("Your BMI is: ");
System.out.println(
  Math.round(bodyMassIndex));

}
}
參考: me
2009-08-03 8:24 am
This should work with main function...

import java.io.*;

// program to calculate Body Mass Index (BMI)
public class CalculateBMI {
public void start() {

try {

InputStreamReader inp = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inp);

System.out.print("Enter your weight in Kilograms: ");
Double weight = Double.parseDouble(br.readLine());
System.out.print("Enter your height in Metres: ");
double height = Double.parseDouble(br.readLine());
double bodyMassIndex = weight / (height * height);
System.out.print("Your BMI is: ");
System.out.println(Math.round( bodyMassIndex));

} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] a) {
CalculateBMI cb = new CalculateBMI();
cb.start();
}

}


收錄日期: 2021-04-29 20:27:25
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090802223639AAZYvPK

檢視 Wayback Machine 備份