Java 在公式中键入值是硬编码吗?

Java 在公式中键入值是硬编码吗?,java,hard-coding,Java,Hard Coding,我正在做一个课堂作业,本周的课文中涉及到的一件事就是硬编码,以及它是如何受到反对的 我的问题是,如果我实际输入的值是公式的一部分,这算是硬编码吗 示例:球体的体积是(4/3)*PI*r^3我应该在块代码的开头声明(4/3)为变量吗?或者我应该更进一步,宣布整个公式 我希望我所问的对每个人都有意义,这里有一些额外的信息是我的代码: package area_volume_surfacearea; /** * Area_Volume_SurfaceArea (Assignment numbe

我正在做一个课堂作业,本周的课文中涉及到的一件事就是硬编码,以及它是如何受到反对的

我的问题是,如果我实际输入的值是公式的一部分,这算是硬编码吗

示例:球体的体积是(4/3)*PI*r^3我应该在块代码的开头声明(4/3)为变量吗?或者我应该更进一步,宣布整个公式

我希望我所问的对每个人都有意义,这里有一些额外的信息是我的代码:

  package area_volume_surfacearea;

/**
 * Area_Volume_SurfaceArea (Assignment number 9)
 * For CSCI 111
 * last modified sept 15 12 p.m.
 * @author Jeffrey Quinn
 */
        //import Scanner class
import java.util.Scanner;

public class Area_Volume_SurfaceArea 
{

    /**
     * This method finds the Area, Volume and Surface Area of geometric shapes 
     based off a value that the user inputs (in inches)
     */


    public static void main(String[] args) 
    {
        double distance; //the distance the users inputs to be used in the formulas (raidus or side)
        double areaCircle; //the area of the circle
        double areaSquare; //the area of the square
        double volumeSphere; //the volume of the sphere
        double volumeCube; //the volume of the cube
        double surfaceAreaSphere; // the surface area of the sphere
        double surfaceAreaCube; //the surface area of the cube

        //declare an instance of Scanner class to read the datastream from the keyboard
        Scanner keyboard = new Scanner(System.in);

        //get the value of the radius of the circle (in inches)
        System.out.println("Please enter a distance (in inches) to be used:");
        distance = keyboard.nextDouble();

        //calculate area of a circle (in inches)
        areaCircle = Math.PI * Math.pow(distance,2);

        //calculate area of a square (in inches)
        areaSquare = Math.pow(distance,2);

        //calculate volume of a sphere (in inches)
        volumeSphere = (4/3) * Math.PI * Math.pow(distance,3);

        //calculate volume of a cube (in inches)
        volumeCube = Math.pow(distance,3);

        // calulate surface area of a sphere (in inches)
        surfaceAreaSphere = 4 * Math.PI * Math.pow(distance,2);

        //calculate surface area of a cube (in inches)
        surfaceAreaCube = 6 * Math.pow(distance,2);

        //results
        System.out.println(" The area of a circle with the radius of " + distance + "in, is " + areaCircle);
        System.out.println(" The area of a square whos sides measures " + distance+ "in, is " + areaSquare);
        System.out.println(" The volume of a sphere with the radius of " + distance + "in, is " + volumeSphere);
        System.out.println(" The volume of a cube whos sides measures " + distance + "in, is " + volumeCube);
        System.out.println(" The surface area of a sphere with the radius of " + distance + "in, is " + surfaceAreaSphere);
        System.out.println(" The surface area of a cube whos sides measures " + distance + "in, is " + surfaceAreaCube); 
    } //end main()

}// end class Area_Volume_SurfaceArea

本着自我记录代码的精神,您可以创建一个变量:

double SPHERE_VOLUME_RATIO = 4.0d/3.0d;

顺便说一句,应用你的研究,你的代码很容易阅读。

这些公式永远不会改变。硬编码这样的算法绝对没有问题。添加不必要的变量只会使代码更加复杂,而不会带来任何好处。然而,干代码同样重要,所以如果您发现自己在许多地方反复编写这个比率,那么最好将其存储在变量中。例如,如果我必须在代码中的许多地方使用数字666,即使这个数字永远不会改变,我会说最好将它定义为常量,因为它不太容易出错


野兽的最终整数=666

注意,
(4/3)
是整数除法。在这种情况下,对于
球体
正方形
等,应该有不同的类,并且在这些类中分别有计算体积和面积的逻辑。一般经验法则:如果没有合理的变量名称可供使用,硬编码可能很好(或者整个事情都不好)。哇,快评论,谢谢大家:D但是@Rohit Jain也要记住(我以前应该说过)这是一门入门级课程,这只是我们的第二个作业,我们还没有深入到很深的程度,也没有真正制作我们自己的课程。@JeffreyQuinn。哦那没问题。您现在可以忽略我的评论。:)它应该是双球体体积比=4.0/3.0;编辑:自动修复它。