繁体   English   中英

有什么办法可以更好地重写我的方法吗?

[英]Is there any way I can rewrite my methods better?

我不完全确定这个问题是否有更简单的答案,我正在认真考虑它或什么,但我目前正在编写一个矩形块程序来练习 Java。 它的结构有 4 个方法: getInputvolBlocksaBlockdisplay ,我只想对这些方法使用局部变量。 有没有一种方法可以利用 getInput 接受并返回来自用户的单个双精度值,如果是这样,我如何在其他方法中使用该输入?

我构建了这段代码,它在 getInput() 中使用局部变量,然后将这些值传递给其他方法,但我无法找出显示方法,因此我将其硬编码到计算方法本身中。

这是代码:

import java.util.*;
public class Block {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String choice = "Y";
        while (choice.equals("Y")){
            getInput();
            System.out.println("Would you like to do another calculation?(Y/N): ");
            choice = in.next().toUpperCase();
        }
        System.out.println("Program now ending...");
    }

    public static void getInput() {
        double l, w, h;
        Scanner fin = new Scanner(System.in);
        System.out.println("Please enter the length, width, and height in that order: ");
        l = fin.nextDouble();
        w = fin.nextDouble();
        h = fin.nextDouble();

        volBlock(l, w, h);
        surfaceAreaBlock(l,w,h);
    }

    public static void volBlock(double length, double width, double height) {
        double volume;

        volume = length * width * height;

        System.out.println("The volume is: " + volume);
    }

    public static void surfaceAreaBlock (double l, double w, double h) {
        double surfaceArea;

        surfaceArea = 2 * (l*h+l*w+h*w);

        System.out.println("The surface area is: " + surfaceArea);
    }
}

如果这个问题有点乱,我很抱歉,我很难弄清楚所有这些。 我对Java很陌生。

任何帮助表示赞赏,谢谢!

如果您正在练习 Java,那么在进一步学习之前,您应该更熟悉面向对象编程,因为您的代码让我相信您更习惯于过程语言(例如 C、C++ 等)。 Java 不依赖于在其主要的几个静态辅助方法; 首选方法是构建一些类来为您执行这些计算,然后将这些函数创建的结果用于基本输入/输出,这通常是 main 的用途。

我实现了一个块类来演示我的意思:

public class Block {
    private double length;
    private double width;
    private double height;

    public Block(double l, double w, double h) {
        length = l;
        width = w;
        height = h;
    }

    public double getVolume() {
        return length * width * height;
    }

    public double getSurfaceArea() {
        return 2 * length * (height + width) + height * width;
    }

    /* This is the "display" method that you want */
    public String toString() {
        return "The volume is: " + getVolume() + "\n"
               "The surface area is: " + getSurfaceArea();
    }
}

使用Block类,您的主要内容变得更加简单:

public static void main() {
    Scanner in = new Scanner(System.in);
    char choice = 'y';

    do {
        System.out.print("Please enter the dimensions of the block: ");
        double length = in.nextDouble();
        double width  = in.nextDouble();
        double height = in.nextDouble();
        Block block = new Block(length, width, height);

        System.out.println(block);
        System.out.print("continue (y/n)? ");
        choice = in.nextLine.toLowerCase().charAt(0);
    } while (choice == 'y');
}

如果您从 getInput()、volBlock() 和 surfaceAreaBlock() 方法返回值,您可能能够根据需要构建其余部分。

例如surfaceAreaBlock变成:

public static double surfaceAreaBlock (double l, double w, double h){
    double surfaceArea;

    surfaceArea = 2 * (l*h+l*w+h*w);

    return surfaceArea;
}

然后当您调用 surfaceAreaBlock 时,您可以执行以下操作:

public static void main(String[] args) {
    ...

    double surfaceArea = surfaceAreaBlock();

    // Do something with the surface area in this method
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM