diff --git a/Hello.java b/Hello.java index c19662a716f3a307415a09065c03ea211307fcc4..700f65bc5ad9674a897e9067fd183834d18d2353 100644 --- a/Hello.java +++ b/Hello.java @@ -1,19 +1,36 @@ +/** + * Hello类,演示基本的Java程序结构 + * 包含主方法入口、简单的求和运算以及斐波那契数列生成功能 + */ public class Hello { + /** + * 程序的主入口方法 + * 创建Hello对象,调用sum方法计算两个整数的和,并格式化输出结果 + * + * @param args 命令行参数,本程序未使用 + */ public static void main(String[] args) { - int x = 5, y = 6; + int x = 5, y = 6; // 定义两个待求和的整数 - Hello bigHello = new Hello(); - int result = bigHello.sum(x, y); - String buf = String.format("The sum is %d", result); - System.out.println(buf); + Hello bigHello = new Hello(); // 创建Hello类的实例 + int result = bigHello.sum(x, y); // 调用sum方法计算两数之和 + String buf = String.format("The sum is %d", result); // 格式化输出字符串 + System.out.println(buf); // 打印求和结果 } + /** + * 计算两个整数的和 + * + * @param a 第一个加数 + * @param b 第二个加数 + * @return 两个参数的和 + */ private int sum(int a, int b) { - return a+b; + return a+b; // 返回两数之和 } /** @@ -27,26 +44,27 @@ public class Hello */ public static long[] generateFibonacciSequence(long start, int length) { if (start < 0) { - throw new IllegalArgumentException("起始数字不能为负数"); + throw new IllegalArgumentException("起始数字不能为负数"); // 校验起始数字非负 } if (length <= 0) { - throw new IllegalArgumentException("长度必须大于0"); + throw new IllegalArgumentException("长度必须大于0"); // 校验长度必须为正数 } - long[] fibonacci = new long[length]; + long[] fibonacci = new long[length]; // 初始化结果数组 if (length == 1) { - fibonacci[0] = start; + fibonacci[0] = start; // 长度为1时,仅返回起始数字 return fibonacci; } fibonacci[0] = start; fibonacci[1] = start; // 斐波那契数列的前两个数字相同 + // 从第三个位置开始,每个数字为前两个数字之和 for (int i = 2; i < length; i++) { fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; } - return fibonacci; + return fibonacci; // 返回生成的斐波那契数列 } } \ No newline at end of file