花了几天时间学习Java(我的第一语言),我只是想知道是否有更有效的方式来编写此代码?
该程序的主要目标是计算某人的BMI。
它询问用户的姓名,询问他们是否要使用公制或英制,询问测量值,并提供带有小图表的答案,以供用户查看适合的位置。
目前可以正常工作,但是我试图找出方法来压缩代码并使之更有效(纯粹出于学习目的)
import java.util.Scanner; //importing the java library utilities to use the scan functions
public class Bmi0 //declaring my intitial class and making it a public class
{
public static void main(String[] args) //my main method
{
Scanner input = new Scanner(System.in); // starts the scanner utility to take input fromm the command line
float height; //declaring a variable
float weight; //declaring a variable
float bmi; //declaring a variable
String option; //declaring a variable
String name; //declaration of a variable called name of type string
System.out.printf("%nWhat is your name? ");//prompts the user for their name
name = input.nextLine(); //reads the users input and saves it to the variable name
System.out.printf("%nHi %s, Let's calculate you BMI %n" , name); //displays what the user
//typed using a %s as the place holder for the value that is in the name variable
System.out.printf("%nUse Imperial or Metric? (Format: I or M) "); //printing to the command window and asking for input
option = input.nextLine(); //placing the user input into the height variable
if (option.equals ("M")) //if option is M for metric do the following
{
System.out.printf("%nWhat is your height in Meters? "); //printing to the command window and asking for input
height = input.nextFloat(); //placing the user input into the height variable
System.out.print("What is your weight in Kilos? "); //printing to the command window and asking for input
weight = input.nextFloat(); //placing the user input into the weight variable
bmi = (weight / (height * height)); //calculates the conversion and stores it in the variable
System.out.printf("%nOk %s, your BMI is: %.2f%n%n" , name, bmi); //displays the answer to 2 decimal places
System.out.printf("BMI Categories:%n Underweight = <18.5 %n Normal weight = 18.5 - 24.9 %n Overweight = 25 - 29.9 %n Obesity = BMI of 30 or greater %n%n%n");
}
else if (option.equals ("I")) //if the imperial option is chosen
{
System.out.printf("%nWhat is your height in Inches? "); //printing to the command window and asking for input
height = input.nextFloat(); //placing the user input into the height variable
System.out.printf("What is your weight in Pounds? "); //printing to the command window and asking for input
weight = input.nextFloat(); //placing the user input into the weight variable
bmi = (weight / (height * height) * 703) ; //calculates the conversion and stores it in the variable
System.out.printf("%nOk %s, your BMI is: %.2f%n%n" , name, bmi); //displays the answer to 2 decimal places
System.out.printf("BMI Categories:%n Underweight = <18.5 %n Normal weight = 18.5 - 24.9 %n Overweight = 25 - 29.9 %n Obesity = BMI of 30 or greater %n%n%n");
}
} //end of the method
}//end of the class
答案 0 :(得分:0)
一些想法:
//importing the java library utilities to use the scan functions
...不会告诉读者import java.util.Scanner
尚未放弃的任何内容。 太多的代码中的信息适得其反。这会使读者浪费时间和精力在无关紧要的细节上。相反:以明确意图的方式编写代码,无需。 {
保留在if条件的行上。不要不在括号后面添加另一个空行。而是用2或4个空格缩进新块。目前,一种非常著名的方法是google中的一种方法。对于新手来说,您的代码“还可以”,但是正如所说的那样:大多数注释都可以扔掉,这将提高的可读性。接下来的改进是将不同的“方面”放入较小的辅助方法中,以避免将所有内容都包含在一个冗长的主方法中。