java几天时间,正在寻找效率建议
我花了几天时间学习Java(我的第一语言),我只是想知道是否有更有效的方法来编写这段代码
这个项目的主要目标是计算一些人的体重指数
它询问用户的姓名,询问他们是否愿意使用公制或英制,询问测量值,并提供一个小图表,供用户查看其适合的位置
目前它还可以正常工作,但我正试图找出压缩代码并使其更高效的方法(纯粹是为了学习)
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
# 1 楼答案
一些想法:
//importing the java library utilities to use the scan functions
。。。这不会告诉读者任何import java.util.Scanner
还没有透露的东西太多代码中的大量信息会适得其反。这会让读者把时间和精力浪费在无关紧要的细节上。取而代之的是:以明确意图的方式编写代码,不需要大量注释李>{
。不要在大括号后再添加一条空行。相反,使用2或4个空格缩进新块。现在有一个非常著名的方法是来自google的方法李>对于新手来说,您的代码“还可以”,但正如前面所说的:大多数注释都可以扔掉,这将提高可读性。下一个改进是将不同的“方面”放入较小的助手方法中,以避免将所有内容都放在一个冗长的主方法中