我刚学习 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
最佳答案
一些想法:
- 评论太多。
//导入java库实用程序以使用扫描功能
...这并没有告诉读者任何import java.util.Scanner
尚未透露的信息。代码中太多信息会适得其反。它使读者将时间和精力浪费在无关紧要的细节上。相反:以明确意图的方式编写代码,不需要大量注释。 - 格式:遵循标准 Java 编码风格指南。例如:将开头
{
保留在 if 条件行上。不要不要在大括号后添加另一个空行。相反,用 2 或 4 个空格缩进新 block 。目前一种非常著名的方法是 google 中的方法。 .
对于新手来说,您的代码“还可以”,但正如所说:大多数注释都可以被丢弃,这将提高可读性。下一个改进是将不同的“方面”放入较小的辅助方法中,以避免将所有内容都放在一个冗长的主方法中。
关于java - 几天后正在寻求效率建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54515785/