java - 几天后寻找效率建议
问题描述
我花了几天时间学习 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
解决方案
一些想法:
- 评论太多。
//importing the java library utilities to use the scan functions
...这并没有告诉读者任何import java.util.Scanner
尚未放弃的东西。代码中的太多信息会适得其反。它使读者将时间和精力浪费在无关紧要的细节上。相反:以明确意图的方式编写代码,而不需要大量注释。 - 格式:遵循标准的 java 编码风格指南。喜欢:例如,将开口保持
{
在 if 条件的行上。不要在大括号后添加另一个空行。相反,将新块缩进2 或 4 个空格。目前一种非常著名的方法是来自google的方法。
您的代码对于新手来说是“好的”,但正如所说:大多数评论都可能被丢弃,这将提高可读性。下一个改进是将不同的“方面”放入较小的辅助方法中,以避免将所有内容都放在一个冗长的主要方法中。
推荐阅读
- javascript - Javascript/Node.js 服务器 - 在 JSON 文件中查询数据集以创建 URL
- haskell - 'runhaskell' 不是内部或外部命令、可运行程序或批处理文件
- visual-studio - 无法在 Visual Studio 2019 中访问 Android Wear Xamarin.Android 模板
- flutter - 我的代码中有错误我一直尝试但它不起作用我使用颤振降级命令颤振清洁等
- java - org.junit 无法解决 错误继续。但是添加jar文件
- php - 我在 Xampp 中安装了 Anubis “但我在索引页面和 botstable 文件中有错误
- java - 为 Sub4Sub 应用程序构建 .apk 时出错
- javascript - Javascript Array of Objects Sort
- java - 如何在Android中写入外部存储而不是内部存储?
- c# - 在更大的文本集中的某个点插入字符串