说明:共 20 道练习题,围绕 Java 方法的 4 种声明方式 展开,每种类型 5 道题。
方法 4 种类型:
- 无参无返回值(
void+ 无参)- 有参无返回值(
void+ 有参)- 有参有返回值(有返回值 + 有参)
- 无参有返回值(有返回值 + 无参)
方法特点:
public static void 方法名() { ... }— 不接收参数,也不返回结果,仅执行一段代码。
难度:⭐
知识点:无参无返回值方法
定义一个方法 printLine,调用后打印一条由 - 组成的 40 个字符的分隔线。
示例输出:
----------------------------------------
参考代码:
public class Practice01 {
public static void printLine() {
for (int i = 0; i < 40; i++) {
System.out.print("-");
}
System.out.println();
}
public static void main(String[] args) {
printLine();
}
}
难度:⭐⭐
知识点:无参无返回值方法、嵌套循环
定义一个方法 multiplicationTable,调用后输出 9×9 乘法表。
示例输出:
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
...
参考代码:
public class Practice02 {
public static void multiplicationTable() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "×" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
multiplicationTable();
}
}
难度:⭐
知识点:无参无返回值方法
定义一个方法 greeting,根据当前系统时间输出问候语(上午输出"早上好",下午输出"下午好",晚上输出"晚上好")。
提示:可以用
java.time.LocalTime.now()获取当前时间。
参考代码:
import java.time.LocalTime;
public class Practice03 {
public static void greeting() {
int hour = LocalTime.now().getHour();
if (hour >= 6 && hour < 12) {
System.out.println("早上好!");
} else if (hour >= 12 && hour < 18) {
System.out.println("下午好!");
} else {
System.out.println("晚上好!");
}
}
public static void main(String[] args) {
greeting();
}
}
难度:⭐
知识点:无参无返回值方法、循环
定义一个方法 printEven,调用后输出 1~100 之间的所有偶数,每行输出 10 个数。
示例输出:
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
...
参考代码:
public class Practice04 {
public static void printEven() {
int count = 0;
for (int i = 2; i <= 100; i += 2) {
System.out.printf("%3d", i);
count++;
if (count % 10 == 0) {
System.out.println();
}
}
}
public static void main(String[] args) {
printEven();
}
}
难度:⭐⭐
知识点:无参无返回值方法
定义一个方法 showMenu,调用后在控制台打印一个简易计算器菜单。
示例输出:
========== 简易计算器 ==========
1. 加法
2. 减法
3. 乘法
4. 除法
0. 退出
================================
参考代码:
public class Practice05 {
public static void showMenu() {
System.out.println("========== 简易计算器 ==========");
System.out.println(" 1. 加法");
System.out.println(" 2. 减法");
System.out.println(" 3. 乘法");
System.out.println(" 4. 除法");
System.out.println(" 0. 退出");
System.out.println("================================");
}
public static void main(String[] args) {
showMenu();
}
}
方法特点:
public static void 方法名(参数列表) { ... }— 接收参数处理,但不返回结果。
难度:⭐
知识点:有参无返回值方法
定义一个方法 judgeEven,接收一个整数参数,判断并输出它是奇数还是偶数。
示例:
调用:judgeEven(7)
输出:7 是奇数
调用:judgeEven(10)
输出:10 是偶数
参考代码:
public class Practice06 {
public static void judgeEven(int num) {
if (num % 2 == 0) {
System.out.println(num + " 是偶数");
} else {
System.out.println(num + " 是奇数");
}
}
public static void main(String[] args) {
judgeEven(7);
judgeEven(10);
}
}
难度:⭐⭐⭐
知识点:有参无返回值方法、嵌套循环、质数判断
定义一个方法 printPrime,接收一个整数 n,输出 2 ~ n 之间的所有质数,每行 5 个。
示例:
调用:printPrime(20)
输出: 2 3 5 7 11
13 17 19
参考代码:
public class Practice07 {
public static void printPrime(int n) {
int count = 0;
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.printf("%3d", i);
count++;
if (count % 5 == 0) {
System.out.println();
}
}
}
}
public static void main(String[] args) {
printPrime(20);
}
}
难度:⭐⭐
知识点:有参无返回值方法、嵌套循环
定义一个方法 printTriangle,接收一个整数 n,打印 n 层的直角三角形(由 * 组成)。
示例:
调用:printTriangle(5)
输出:
*
**
***
****
*****
参考代码:
public class Practice08 {
public static void printTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
printTriangle(5);
}
}
难度:⭐⭐⭐
知识点:有参无返回值方法、空格和星号的规律
定义一个方法 printIsosceles,接收一个整数 n,打印 n 层的等腰三角形。
示例:
调用:printIsosceles(5)
输出:
*
***
*****
*******
*********
参考代码:
public class Practice09 {
public static void printIsosceles(int n) {
for (int i = 1; i <= n; i++) {
// 打印空格
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// 打印星号
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
printIsosceles(5);
}
}
难度:⭐
知识点:有参无返回值方法
定义一个方法 printLine,接收两个参数:字符 ch 和数量 count,打印由 ch 组成的 count 个字符的分隔线。
示例:
调用:printLine('=', 30)
输出:==============================
调用:printLine('*', 15)
输出:***************
参考代码:
public class Practice10 {
public static void printLine(char ch, int count) {
for (int i = 0; i < count; i++) {
System.out.print(ch);
}
System.out.println();
}
public static void main(String[] args) {
printLine('=', 30);
printLine('*', 15);
}
}
方法特点:
public static 返回值类型 方法名(参数列表) { ... return 值; }— 接收参数并返回处理结果。
难度:⭐
知识点:有参有返回值方法
定义一个方法 sum,接收两个整数参数,返回它们的和。
示例:
调用:sum(10, 20)
返回:30
参考代码:
public class Practice11 {
public static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = sum(10, 20);
System.out.println("10 + 20 = " + result);
}
}
难度:⭐⭐
知识点:有参有返回值方法、三元运算符
定义一个方法 maxOfThree,接收三个整数参数,返回其中的最大值。
示例:
调用:maxOfThree(7, 15, 9)
返回:15
参考代码:
public class Practice12 {
public static int maxOfThree(int a, int b, int c) {
int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
return max;
}
public static void main(String[] args) {
int max = maxOfThree(7, 15, 9);
System.out.println("最大值是:" + max);
}
}
难度:⭐⭐
知识点:有参有返回值方法、浮点数运算
定义一个方法 circleArea,接收一个 double 类型的半径 r,返回圆的面积(π 取 3.1415926)。
示例:
调用:circleArea(5.0)
返回:78.539815
公式:面积 = π × r²
参考代码:
public class Practice13 {
public static double circleArea(double r) {
double pi = 3.1415926;
return pi * r * r;
}
public static void main(String[] args) {
double area = circleArea(5.0);
System.out.printf("半径为 5.0 的圆面积:%.2f%n", area);
}
}
难度:⭐⭐
知识点:有参有返回值方法、boolean 返回值
定义一个方法 isLeapYear,接收一个整数年份,如果是闰年返回 true,否则返回 false。
闰年规则:能被 4 整除但不能被 100 整除,或者能被 400 整除。
示例:
调用:isLeapYear(2024)
返回:true
调用:isLeapYear(2025)
返回:false
参考代码:
public class Practice14 {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static void main(String[] args) {
System.out.println("2024 年是闰年吗?" + isLeapYear(2024));
System.out.println("2025 年是闰年吗?" + isLeapYear(2025));
}
}
难度:⭐⭐
知识点:有参有返回值方法、循环累积
定义一个方法 factorial,接收一个整数 n,返回 n 的阶乘(n!)。
公式:n! = 1 × 2 × 3 × ... × n,且 0! = 1
示例:
调用:factorial(5)
返回:120 (1×2×3×4×5=120)
调用:factorial(0)
返回:1
参考代码:
public class Practice15 {
public static long factorial(int n) {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
System.out.println("5! = " + factorial(5));
System.out.println("0! = " + factorial(0));
}
}
方法特点:
public static 返回值类型 方法名() { ... return 值; }— 不接收参数,但返回一个结果。
难度:⭐
知识点:无参有返回值方法
定义一个方法 getHour,返回当前系统时间的整点小时数(0~23)。
示例(假设现在是下午 3 点):
调用:getHour()
返回:15
参考代码:
import java.time.LocalTime;
public class Practice16 {
public static int getHour() {
return LocalTime.now().getHour();
}
public static void main(String[] args) {
int hour = getHour();
System.out.println("现在时间是:" + hour + "点");
if (hour >= 6 && hour < 12) {
System.out.println("上午好!");
} else if (hour >= 12 && hour < 18) {
System.out.println("下午好!");
} else {
System.out.println("晚上好!");
}
}
}
难度:⭐
知识点:无参有返回值方法、随机数
定义一个方法 getLuckyNumber,生成并返回一个 1~100 之间的随机整数作为幸运数字。
参考代码:
public class Practice17 {
public static int getLuckyNumber() {
return (int)(Math.random() * 100) + 1;
}
public static void main(String[] args) {
int lucky = getLuckyNumber();
System.out.println("今天的幸运数字是:" + lucky);
}
}
难度:⭐
知识点:无参有返回值方法、常量返回
定义一个方法 getPi,返回圆周率值 3.1415926(double 类型)。
参考代码:
public class Practice18 {
public static double getPi() {
return 3.1415926;
}
public static void main(String[] args) {
double pi = getPi();
System.out.println("圆周率 π = " + pi);
// 使用返回的 π 值计算半径为 3 的圆面积
double area = pi * 3 * 3;
System.out.printf("半径为 3 的圆面积 = %.2f%n", area);
}
}
难度:⭐⭐
知识点:无参有返回值方法、随机数
定义一个方法 rollDice,模拟抛一颗六面骰子,返回 1~6 之间的随机整数。
参考代码:
public class Practice19 {
public static int rollDice() {
return (int)(Math.random() * 6) + 1;
}
public static void main(String[] args) {
// 模拟抛两次骰子
int first = rollDice();
int second = rollDice();
System.out.println("第一次抛骰子:" + first);
System.out.println("第二次抛骰子:" + second);
System.out.println("两次之和:" + (first + second));
}
}
难度:⭐⭐
知识点:无参有返回值方法、返回数组
定义一个方法 getRandomColor,生成并返回一个包含 3 个元素的 int[] 数组,分别代表 RGB 颜色值(每个分量 0~255)。
示例:
调用:getRandomColor()
返回:[128, 45, 200] (每次结果随机)
参考代码:
import java.util.Arrays;
public class Practice20 {
public static int[] getRandomColor() {
int[] rgb = new int[3];
rgb[0] = (int)(Math.random() * 256); // R
rgb[1] = (int)(Math.random() * 256); // G
rgb[2] = (int)(Math.random() * 256); // B
return rgb;
}
public static void main(String[] args) {
int[] color = getRandomColor();
System.out.println("随机颜色 RGB = " + Arrays.toString(color));
}
}
| 类型 | 声明格式 | 特点 | 适用场景 |
|---|---|---|---|
| 无参无返回值 | public static void 方法名() {} |
不接收数据,不返回数据 | 打印菜单、输出固定内容 |
| 有参无返回值 | public static void 方法名(参数) {} |
接收数据,不返回数据 | 打印 n 层三角形、判断并输出奇偶 |
| 有参有返回值 | public static 类型 方法名(参数) {} |
接收数据,返回处理结果 | 求和、求最值、计算面积 |
| 无参有返回值 | public static 类型 方法名() {} |
不接收数据,返回结果 | 获取随机数、获取系统时间 |