displayresult.java

来自「21天学通JAVA2(第三版)(专业参考版)」· Java 代码 · 共 29 行

JAVA
29
字号
public class DisplayResult {
    public DisplayResult(String input) {
        try {
            float in = Float.parseFloat(input);
            Squared sq = new Squared(in);
            float result = sq.value;
           System.out.println("The square of " + input + " is " + result);
        } catch (NumberFormatException nfe) {
            System.out.println(input + " is not a valid number.");
        }
    }

    class Squared {
        float value;

        Squared(float x) {
            value = x * x;
        }
    }

    public static void main(String[] arguments) {
        if (arguments.length < 1) {
            System.out.println("Usage: java DisplayResult number");
        } else {
            DisplayResult dr = new DisplayResult(arguments[0]);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?