Scanner sc = new Scanner(System.in);
int sum = 0;
int num = 0;
while (sc.hasNextInt()) {
int tmp = sc.nextInt();
sum += tmp;
num++;
}
System.out.println("sum = " + sum);
System.out.println("avg = " + sum / num);
sc.close();
public class TestMethod {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("交换前: a = " + a + " b = " + b);
swap(a, b);
System.out.println("交换后: a = " + a + " b = " + b);
}
public static void swap(int x, int y) {
int tmp = x;
x = y;
y = tmp;
}
}
// 运行结果
交换前: a = 10 b = 20
交换后: a = 10 b = 20
原因分析:实参 a 和 b 是 main 方法中的变量,空间在 main 方法的栈中;形参 x 和 y 是 swap 方法中的变量,两者空间独立。在 swap 调用时,只是将实参的值拷贝了一份传给形参,因此对形参的操作不会影响实参。
2.7 方法重载
为什么需要方法重载?
当既想计算两个整数相加,又想计算两个小数相加时,会出现参数类型不匹配的问题。方法重载可以完美解决!
public class TestMethod {
public static void main(String[] args) {
add(1, 2); // 调用add(int, int)
add(1.5, 2.5); // 调用add(double, double)
add(1.5, 2.5, 3.5); // 调用add(double, double, double)
}
public static int add(int x, int y) {
return x + y;
}
public static double add(double x, double y) {
return x + y;
}
public static double add(double x, double y, double z) {
return x + y + z;
}
}
public static void main(String[] args) {
int n = 5;
int ret = factor(n);
System.out.println("ret = " + ret);
}
public static int factor(int n) {
if (n == 1) {
return 1;
}
return n * factor(n - 1); // factor 调用函数自身
}
// 执行结果
ret = 120
更多递归练习
1. 按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4)
public static void print(int num) {
if (num > 9) {
print(num / 10);
}
System.out.println(num % 10);
}
2. 递归求 1 + 2 + 3 + … + 10
public static int sum(int num) {
if (num == 1) {
return 1;
}
return num + sum(num - 1);
}
3. 求斐波那契数列的第 N 项
public static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
}
return fib(n - 1) + fib(n - 2);
}
public class TestStudent {
public static void main(String[] args) {
int score1 = 70;
int score2 = 80;
int score3 = 85;
int score4 = 60;
int score5 = 90;
// ... 逐个输出
}
}
如果有100个学生呢?创建100个变量?数组就是用来解决这个问题的!
3.3 什么是数组?
数组:可以看成是相同类型元素的一个集合,在内存中是一段连续的空间。
关键特点:
数组的空间是连在一起的
每个空间有自己的编号,起始位置的编号为0,即数组的下标
3.4 数组的创建及初始化
数组的创建
T[] 数组名 = new T[N];
T:表示数组中存放元素的类型
T[]:表示数组的类型
N:表示数组的长度
int[] array1 = new int[10]; // 容纳10个int类型元素的数组
double[] array2 = new double[5]; // 容纳5个double类型元素的数组
String[] array3 = new String[3]; // 容纳3个字符串元素的数组
数组的初始化
1. 动态初始化:在创建数组时,直接指定数组中元素的个数
int[] array = new int[10];
2. 静态初始化:在创建数组时直接指定具体的数据内容
int[] array1 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
double[] array2 = new double[]{1.0, 2.0, 3.0, 4.0, 5.0};
String[] array3 = new String[]{"hello", "Java", "!!!"};
public static int binarySearch(int[] arr, int toFind) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (toFind < arr[mid]) {
right = mid - 1; // 去左侧区间找
} else if (toFind > arr[mid]) {
left = mid + 1; // 去右侧区间找
} else {
return mid; // 找到了
}
}
return -1; // 没找到
}
冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
int tmp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = tmp;
}
}
}
}
Java 内置了更高效的排序:Arrays.sort(arr);
数组逆序
public static void reverse(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
left++;
right--;
}
}
import java.util.Random;
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Random random = new Random();
Scanner sc = new Scanner(System.in);
int toGuess = random.nextInt(100);
while (true) {
System.out.println("请输入要输入的数字: (1-100)");
int num = sc.nextInt();
if (num < toGuess) {
System.out.println("低了");
} else if (num > toGuess) {
System.out.println("高了");
} else {
System.out.println("猜对了");
break;
}
}
sc.close();
}
}
public class Method {
// 方法定义
public static boolean isLeapYear(int year) {
if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) {
return true;
} else {
return false;
}
}
}
public class Method {
// 方法的定义
public static int add(int x, int y) {
return x + y;
}
}
序号
注意点
说明
1
修饰符
现阶段直接使用 public static 固定搭配
2
返回值类型
如果有返回值必须与返回实体类型一致,没有必须写 void
3
方法名字
采用小驼峰命名
4
参数列表
无参数则()为空,有参数需指定类型,多个参数用逗号隔开
5
方法体
方法内部要执行的语句
6
在Java中,方法必须写在类当中
7
在Java中,方法不能嵌套定义
8
在Java中,没有方法声明一说
public class Method {
// 方法的定义
public static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
// 调用方法
int ret = add(1, 2);
System.out.println("ret = " + ret);
int ret2 = add(3, 4);
System.out.println("ret2 = " + ret2);
}
}