gpt4 book ai didi

java - 代码格式、结构、一致性

转载 作者:行者123 更新时间:2023-12-01 23:38:15 27 4
gpt4 key购买 nike

您好,我是一名初学者,目前正在尝试学习 java 编程。课本上的问题:

编写一个程序来帮助人们决定是否购买混合动力汽车。你的程序的输入应该是:•新车的成本•预计每年行驶里程•预计汽油价格 •每加仑英里数的效率•5年后预计转售值(value)

计算拥有这辆车五年的总成本。 (为简单起见,我们不会考虑融资成本。)从网上获取新车和二手混合动力车以及类似汽车的实际价格。使用今天的汽油价格和每年 15,000 英里的里程运行您的程序两次。包含伪代码,程序就会根据您的作业运行。

我的问题:我的代码正确,我的程序运行完美。我主要关心的是如何以专业的方式呈现这一点。我如何专业地构建它,我必须做什么才能发布它(例如)。我正在努力养成组织和整齐地呈现我的代码的习惯。任何建议都会有帮助,谢谢!

public class car
{



public static void main(String[] args)

{

Scanner in = new Scanner(System.in);
System.out.println("Car Model: ");
String carModel = in.nextLine();
System.out.print("Cost of Car: ");
int costOfCar = in.nextInt();
System.out.print("The estimated miles driven per year: ");
int milesDriven = in.nextInt();
System.out.print("The estimated gas price: ");
int gasPrice = in.nextInt();
System.out.print("Efficiency in miles per gallon: ");
int milesPerGallon = in.nextInt();
System.out.print("Estimated resale value after 5 years: ");
int retailValue = in.nextInt();



double carEfficiency = (double) gasPrice / milesPerGallon;
double milesDrivenCost = (double) milesDriven * carEfficiency * 5; //5 years of driving
double retailValueInFiveYears = retailValue;
double carUseLoss = costOfCar - retailValueInFiveYears;
double totalCost = carUseLoss + milesDrivenCost;
System.out.print(carModel + " will cost you after 5 years: ");
System.out.format(" %,d%n", Math.round(totalCost));

}
}

最佳答案

  1. 我希望这不是您真正的缩进。
  2. 使用 Java 命名约定。特别是,car 类应该是“Car”。
  3. 我本来想添加一些注释,但变量名称非常具有描述性。
  4. 将 JavaDoc 注释添加到类和 main 方法中。
  5. 始终明确关闭资源。
  6. 用户可能想要输入多个带小数的输入。使用double代替int。扫描仪也将接受不带小数的数字。
  7. 也许您可以包含生成的 JavaDoc HTML 输出。

Java代码:

import java.util.Scanner;

/**
* Computes a car's 5-year cost of ownership.
* Usage:
* java Car
*
* @author Mario Rossi
*/
public class Car {

/**
* Computes a car's 5-year cost of ownership.
*
* @param args Not used.
*/
public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.println("Car Model: ");
String carModel = in.nextLine();
System.out.print("Cost of Car: ");
double costOfCar = in.nextDouble();
System.out.print("Estimated miles driven per year: ");
double milesDriven = in.nextDouble();
System.out.print("Estimated gas price in $ per gallon: ");
double gasPrice = in.nextDouble();
System.out.print("Efficiency in miles per gallon: ");
double milesPerGallon = in.nextDouble();
System.out.print("Estimated resale value after 5 years: ");
double retailValueInFiveYears = in.nextDouble();
in.close();

double carEfficiency = gasPrice / milesPerGallon;
double milesDrivenCost = milesDriven * carEfficiency * 5; //5 years of driving
double carUseLoss = costOfCar - retailValueInFiveYears;
double totalCost = carUseLoss + milesDrivenCost;
System.out.print(carModel + " will cost you after 5 years: ");
System.out.format(" %,.2f%n", totalCost );
}
}

关于java - 代码格式、结构、一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com