学习java第一天
常量分类
常量类型 | 说明 | 举例 |
---|---|---|
字符串常量 | 双括号起来的内容 | “深圳”, “广州” |
整数常量 | 不带小数的数字 | 666, -888 |
小数常量 | 带小数的数字 | 3.14, -789 |
字符常量 | 单引号括起来的内容 | ‘H’, ‘8’, ‘上海’ |
布尔常量 | 布尔值,真或假 | true, false |
空常量 | 空值 | null |
终于写出了没有BUG的程序
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
数据类型
基本数据类型 | |
---|---|
数值型 | 整数(byte,short,int,long),浮点数(float,doule),字符(char) |
非数值型 | 布尔值(boolean) |
引用数据类型 | |
类 | class |
接口 | interface |
数组 | [ ] |
定义变量时类型错误
类型转换
算术运算符
public class HelloWorld {
public static void main(String[] args){
// 算术运算符 + - * / %
int a = 30;
int b = 20;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}
字符 ‘+’ 操作
public class HelloWorld {
public static void main(String[] args){
// 字符串 "+"操作
int i = 1;
String k = "A";
System.out.println(i + k); // 输出1A,对字符串使用 + 时,为字符串的拼接
System.out.println("a" + "k"); // 输出ak
System.out.println("a" + "k" + 4 + 7); // 输出ak47
System.out.println(500 + 20 + "深圳"); // 输出520深圳,从左往右计算,遇到数字时为算术运算符,反之为拼接
// 字符 "+" 操作
char w = 'A';
System.out.println(i + w); // 输出66,字符'A'对应的是计算机底层数值65,A-Z是连续的
w = 'a';
System.out.println(i + w); // 输出98,字符'a'对应的是计算机底层数值97,a-z是连续的
w = '0';
System.out.println(i + w); // 输出49,字符'0'对应的是计算机底层数值48,0-9是连续的
// char c = i + w; 错误写法,因为 i 为int类型,所以只能用int类型接收
int c = i + w;
System.out.println(c);
// int t = 10 + 3.14; 错误写法,因为3.14默认是double类型,比int等级高,所以需要用double来接收
// 等级顺序 byte,short,char->int->long->float->double
double t = 10 + 3.14;
System.out.println(t);
}
}
赋值运算符
public class HelloWorld {
public static void main(String[] args){
// 赋值运算符 = += -= *= /= %=
int i = 10;
System.out.println(i);
i += 10;
System.out.println(i);
i -= 10;
System.out.println(i);
i *= 10;
System.out.println(i);
i /= 10;
System.out.println(i);
i %= 10;
System.out.println(i);
short w = 20;
// w = w + 20; 错误,类型不兼容,因为20默认为int类型,比short等级高
// w = (short)(w + 20); 强制转换为short类型
w += 20;
System.out.println(w);
}
}
自增自减
public class HelloWorld {
public static void main(String[] args){
// 自增自减运算符
int i = 10;
i++;
System.out.println(i); // 输出11
// int j = i++;
// System.out.println(j); // 输出11,当++在后面进行赋值时,先赋值在++
// System.out.println(i); // 输出12
int j = ++i;
System.out.println(i); // 输出12
System.out.println(j); // 输出12,当++在前面进行赋值时,先++在赋值
int m = 10;
m--;
System.out.println(m); // 输出9
// int k = m--;
// System.out.println(k); // 输出9,当--在后面进行赋值时,先赋值在--
// System.out.println(m); // 输出8
int k = --m;
System.out.println(m); // 输出8
System.out.println(k); // 输出8,当--在前面进行赋值时,先--在赋值
}
}
关系运算符
public class HelloWorld {
public static void main(String[] args){
// 关系运算符 == != > < >= <=
int a = 100;
int b = 100;
int c = 300;
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(a != b); // false
System.out.println(a != c); // true
System.out.println(a > b); // false
System.out.println(a > c); // true
System.out.println(a < b); // false
System.out.println(a < c); // true
System.out.println(a >= b); // true
System.out.println(a >= c); // false
System.out.println(a <= b); // true
System.out.println(a <= c); // true
}
}
逻辑运算符
public class HelloWorld {
public static void main(String[] args){
// 逻辑运算符 & | ! ^
int a = 100;
int b = 200;
int c = 300;
// & 且
System.out.println((a > b) & (a > c)); // false & false -> false
System.out.println((a < b) & (a > c)); // true & false -> false
System.out.println((a > b) & (a < c)); // false & true -> false
System.out.println((a < b) & (a < c)); // true & true -> true
// | 或
System.out.println((a > b) | (a > c)); // false & false -> false
System.out.println((a < b) | (a > c)); // true & false -> true
System.out.println((a > b) | (a < c)); // false & true -> true
System.out.println((a < b) | (a < c)); // true & true -> true
// ! 非(取反) !为奇数时表示取反,偶数时结果不变
System.out.println(!(a > b)); // true
System.out.println(!!(a > b)); // false
System.out.println(!!!(a > b)); // true
System.out.println(!!!!(a > b)); // false
// ^ 异或 (两个值相同时为false,否则为true)
System.out.println((a > b) ^ (a > c)); // false & false -> false
System.out.println((a < b) ^ (a > c)); // true & false -> true
System.out.println((a > b) ^ (a < c)); // false & true -> true
System.out.println((a < b) ^ (a < c)); // true & true -> false
}
}
&和&& | 和 || 的区别
public class HelloWorld {
public static void main(String[] args) {
int a = 100;
int b = 200;
int c = 300;
// & 和 && 的区别
// System.out.println((a++ > 100) & (b++ > 100));
// System.out.println(a); // 输出101
// System.out.println(b); // 输出201
// System.out.println((a++ > 100) && (b++ > 100));
// System.out.println(a); // 输出101
// System.out.println(b); // 输出200
// 由此可见 & 无论左边真假,右边都会执行,而 && 判断左边为假时,右边不会再执行
// | 和 || 的区别
// System.out.println((a++ > 100) | (b++ > 100));
// System.out.println(a); // 输出101
// System.out.println(b); // 输出201
System.out.println((++a > 100) || (b++ > 200)); // 由于刚刚写成了a++,是先计算大小再赋值,所以结果一直都是false
System.out.println(a); // 输出101
System.out.println(b); // 输出200
// 由此可见 | 无论左边真假,右边都会执行,而 || 判断左边为真时,右边不会再执行
}
}
三元表达式
public class HelloWorld {
public static void main(String[] args) {
int a = 100;
int b = 200;
int ret = a <= b ? a : b; // 输出100,表达式成立返回 a 的值,反之返回 b
System.out.println(ret);
}
}
public class HelloWorld {
public static void main(String[] args) {
// 已知小明和小红的年龄是 18 和 20 ,判断他们年龄是否相同
int xiaoming = 18;
int xiaohong = 20;
boolean age = xiaoming == xiaohong ? true : false; // 返回false
System.out.println(age);
}
}
public class HelloWorld {
public static void main(String[] args) {
// 判断最大身高值
int xiaoming = 180;
int xiaohong = 170;
int xiaozhang = 175;
int height = xiaoming > xiaohong ? xiaoming : xiaohong ;
int maxheight = height > xiaozhang ? height : xiaozhang;
System.out.println(maxheight);
}
}
输入数据
// 导包
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
// new 一个对象
Scanner sc = new Scanner(System.in);
// 输入身高
int xiaoming = sc.nextInt();
int xiaohong = sc.nextInt();
int xiaozhang = sc.nextInt();
int height = xiaoming > xiaohong ? xiaoming : xiaohong;
int maxheight = height > xiaozhang ? height : xiaozhang;
System.out.println(maxheight);
}
}
流程控制
// 导包
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
// new 一个对象
Scanner sc = new Scanner(System.in);
// 输入数据
int num = sc.nextInt();
if(num % 2 == 0){
System.out.println(num + "是偶数");
}else{
System.out.println(num + "是奇数");
}
}
}
// 导包
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
// new 一个对象
Scanner sc = new Scanner(System.in);
// 输入城市判断地标
String city = sc.nextLine();
// 判断字符串相等用String.equals()方法,不能使用city == "深圳"
if(city.equals("深圳")){
System.out.println("世界之窗");
}else if (city.equals("广州")){
System.out.println("广州塔");
}else if(city.equals("上海")){
System.out.println("东方明珠");
}else{
System.out.println("没有找到地标");
}
}
}
// 导包
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
// 输入成绩判断分数
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score > 100 || score < 0) {
System.out.println("输入错误");
}else if (score >= 90){
System.out.println("优秀");
}else if (score >= 80){
System.out.println("优");
}else if (score >= 70){
System.out.println("良好");
}else if (score >= 60){
System.out.println("一般");
}else {
System.out.println("不及格");
}
}
}
switch语句
public class HelloWorld {
public static void main(String[] args) {
// new 一个对象
Scanner sc = new Scanner(System.in);
// 输入城市判断地标
String city = sc.nextLine();
switch (city) {
case "深圳":
System.out.println("世界之窗");
break;
case "广州":
System.out.println("广州塔");
break;
case "上海":
System.out.println("东方明珠");
break;
case "北京":
System.out.println("故宫");
break;
default:
System.out.println("未找到地标");
}
}
}
// 导包
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
// 通过月份判断季节
// case穿透
switch (month){
case 3:
case 4:
case 5:
System.out.println("春天");
break;
case 6:
case 7:
case 8:
System.out.println("夏天");
break;
case 9:
case 10:
case 11:
System.out.println("秋天");
break;
case 12:
case 1:
case 2:
System.out.println("冬天");
break;
default:
System.out.println("输入错误");
}
}
}
for循环
格式:
for (初始化语句;条件判断语句;条件控制语句) {
循环体
}
// 求1到100间的偶数和
public class HelloWorld {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++){
if (i % 2 == 0){
sum += i;
}
}
System.out.println(sum);
}
}
// 求水仙花数
// 水仙花数就是个位,十位,百位的数字立方的和等于原数
// 首先求出个位,十位,百位的数字是多少
// 个位: 原数字对10取余的结果 例如:153 % 10 = 3
// 十位: 原数字除以10得出的整数结果再对10取余 例如:153 / 10 = 15 15 % 10 = 5
// 百位: 原数字除以100的整数结果在对10取余 例如: 153 / 100 % 10 = 1
// 求任意数字指定位置上的数字 例如:个位数就是对10取余 十位数就是除以10对10取余 百位就是除以100对10取余 以此类推
public class HelloWorld {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
// 计算出每个数字
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 10 /10 % 10;
// 计算三个数字相乘的和是否等于原数字
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.println(i);
}
}
}
}
while循环
// 珠穆朗玛峰的高度为(8844.43米=8844430毫米),如果我有一张纸,厚度为0.1毫米,请问折叠多少次可以折成珠穆朗玛峰的高度
public class HelloWorld {
public static void main(String[] args) {
// 定义计数器
int count = 0;
// 定义纸张厚度
double paper = 0.1;
// 定义珠峰的高度
int height = 8844430;
while (paper <= height) {
// 每次折叠都会加倍,所以*2
paper *= 2;
count += 1;
}
System.out.println(count);
}
}
do…while循环
public class HelloWorld {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("我是好人");
i++;
}while (i <= 5);
}
}
三种循环的区别
for循环和while循环是先判断条件是否成立,然后决定是否执行循环体(先判断后执行)
do...while循环先执行一次循环体,然后判断条件是否成立,是否继续执行循环体(先执行后判断)
for循环和while循环的区别:
条件控制语句所控制的自增变量,是因为归属for循环的语法结构中,在for循环结束后,就不能再次被访问了
条件控制语句所控制的自增变量,是因为不归属while循环的语法结构中,在while循环结束后,变量还能够使用
死循环格式:
for(;;){}
while(true){}
do{}while(true);
random的使用
// 导包
import java.util.Scanner;
import java.util.Random;
// random的使用
// 实现猜数字
public class HelloWorld {
public static void main(String[] args) {
// 创建对象
Random ran = new Random();
// 变量接收随机数
int number = ran.nextInt(100) ;
while (true) {
// 从键盘输入数字
Scanner sc = new Scanner(System.in);
int get_number = sc.nextInt();
if (get_number > number) {
System.out.println("猜大了");
}else if (get_number < number) {
System.out.println("猜小了");
}else {
System.out.println("猜中了");
break;
}
}
}
}
数组(array)
数组是一种用于存储多个相同类型数据的存储模型(类似python里的list)
数组的定义格式
格式一:数据类型[] 变量名 例如:int[] arr 定义了一个int类型的数组,变量名为arr
格式二: 数据类型 变量名[] 例如:int arr[] 定义了一个int类型的变量,变量名为arr数组
public class ArrayDemo {
public static void main(String[] args) {
// 数组在初始化时,会为存储空间添加默认值
// 整数类型 默认值0
// 浮点数类型 默认值0.0
// 布尔值类型 默认值false
// 字符类型 默认值是空字符
// 引用数据类型 默认值是null
// 动态初始化:指定数组长度,不指定值
int[] array = new int[6]; // 定义了一个int类型的数组,数组名是array,数组元素个数是6
System.out.println(array); // 输出xxxxxx,xxxxxx就是数组在内存中的地址
System.out.println(array[1]); // 通过索引访问数组
// 静态初始化:指定值,由系统指定数组长度
// int[] arr = new int[]{1, 2, 3}; 定义了一个int类型的数组,数组名是arr,数组值为1, 2, 3
// 简化格式
int[] arr = {1, 2, 3};
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
// System.out.println(arr[3]); 所以越界ArrayIndexOutOfBoundsException
arr = null;
// System.out.println(arr[0]); arr数组已指向null,所以找不到对应内存地址,造成空指针异常NullPointerException
}
}
数组常见操作
public class ArrayDemo {
public static void main(String[] args) {
// 遍历数组 数组名.length 获取数组长度
int[] arr = {1, 2, 3, 4, 5 ,6, 7, 8, 9};
for (int i = 0;i < arr.length; i++){
System.out.println(arr[i]);
}
}
}
获取数组最大值和最小值
public class ArrayDemo {
public static void main(String[] args) {
// 获取数组最大值
int[] arr = {2, 8, 59, 959, 4, 686, 88, 57, 4};
// 定义变量保存数组的第一个值
int max = arr[0];
// 遍历数组,拿第一个值和后面的值相比较,大于的话就保存到max中
for (int i = 1; i < arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
System.out.println(max);
// 获取数组最小值
int[] arr1 = {2, 8, 59, 959, 4, 686, 88, 57, 4};
// 定义变量保存数组的第一个值
int min = arr[0];
// 遍历数组,拿第一个值和后面的值相比较,小于的话就保存到min中
for (int i = 1; i < arr.length; i++){
if(arr1[i] < min){
min = arr1[i];
}
}
System.out.println(min);
}
}
方法的定义和调用
public class ArrayDemo {
public static void main(String[] args) {
// 调用方法
isEvenNumber(10, 20);
}
// 定义方法
public static void isEvenNumber(int a, int b){
int ret = a > b? a:b;
System.out.println(ret);
}
}
带返回值方法定义(返回的数据类型必须要与方法定义的数据类型一致)
public static 数据类型 方法名(参数){
return 数据;
}
public class ArrayDemo {
public static void main(String[] args) {
// 调用方法
boolean bo = isEvenNumber(99); // 返回的是布尔值,所以要布尔值类型的变量接收
System.out.println(bo);
}
// 定义一个方法,该方法接收一个参数,判断是否为偶数,并返回真假
public static boolean isEvenNumber(int number){
if (number % 2 == 0){
return true;
}else {
return false;
}
}
}
方法重载:多个方法在同一个中,多个方法具有相同的方法名,多个方法的参数不相同,类型不同或数量不同
(可以通过区分不同的参数来调用)
public class ArrayDemo {
public static void main(String[] args) {
System.out.println(test(1, 2));
System.out.println(test((byte) 1, (byte) 2));
System.out.println(test((short) 1, (short) 2));
System.out.println(test((long) 1, (long) 2));
}
public static boolean test(int a, int b){
System.out.println("int");
return a == b;
}
public static boolean test(byte a, byte b){
System.out.println("byte");
return a == b;
}
public static boolean test(short a, short b){
System.out.println("short");
return a == b;
}
public static boolean test(long a, long b){
System.out.println("long");
return a == b;
}
}
// 基本数据类型的参数,形式参数的改变,不影响实际参数的值
public class ArrayDemo {
public static void main(String[] args) {
int number = 100;
System.out.println("调用change方法前:" + number); // 输出100
change(number);
System.out.println("调用change方法后:" + number); // 输出100
}
public static void change(int number){
number = 200;
}
}
// 引用类型的参数,形式参数的改变,影响实际参数的值
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {100, 200, 300};
System.out.println("调用change方法前:" + arr[0]); // 输出100
change(arr);
System.out.println("调用change方法后:" + arr[0]); // 输出200
}
public static void change(int[] arr){
arr[0] = 200; // 内存中数组的值已经发生变化
}
}
// 设计一个方法用于数组遍历,要求遍历的结果是在一行上,输出:[11, 22, 33, 44, 55, 66]
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {11, 22, 33, 44, 55, 66};
printArr(arr);
}
public static void printArr(int[] arr){
System.out.print("[");
for(int i = 0; i < arr.length; i++){
if (i == arr.length - 1){
System.out.print(arr[i]);
}else {
System.out.print(arr[i] + ",");
}
}
System.out.println("]");
}
}
逢七过
// 逢七过(从任意一个数字开始报数,当报的数字包含7或者是7的倍数就跳过)
public class ArrayDemo {
public static void main(String[] args) {
// 遍历1-100之间的数字
for (int i = 1; i <= 100; i++) {
// 根据规则,要么个位数是7,要么十位数是7,要么能够被7整除
if (i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0) {
System.out.println(i);
}
}
}
}
不死神兔
// 不死神兔
// 有一对兔子,从出生后第三个月起每个月都生一对兔子,小兔子到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少
public class ArrayDemo {
public static void main(String[] args) {
// 定义数组存储兔子对数,用动态初始化完成数组元素的初始化,长度为20
int[] arr = new int[20];
// 兔子增加规则
// 1 1 arr[0] = 1
// 2 1 arr[1] = 1
// 3 2 arr[2] = arr[0] + arr[1]
// 4 3 arr[3] = arr[1] + arr[2]
// 5 5 arr[4] = arr[2] + arr[3]
// 因为第一个月,第二个月兔子对数为1
arr[0] = 1;
arr[1] = 1;
// 循环实现每个月的兔子
for (int i = 2; i < arr.length; i++) {
arr[i] = arr[i - 2] + arr[i - 1];
}
// 输出数组最后一位数,就是兔子的对数
System.out.println("第二十个月兔子的对数为:" + arr[19]);
}
}
百钱百鸡
// 百钱百鸡
// 鸡翁五文钱,鸡母三文钱,鸡雏一文钱三只,请问一百文钱鸡翁,鸡母,鸡雏各可以买几只
// 假设鸡翁x只,鸡母y只,鸡雏z只,那么 x + y + z =100 (鸡的数量) 5*x + 3*y + z/3 = 100 (钱数)
// 假设单独看一种鸡型,则
// 0 <= x <= 20
// 0 <= y <=33
// 0 <= z <= 100 理论上鸡雏可以买300只,但是只有一百只鸡
public class ArrayDemo {
public static void main(String[] args) {
// 表示鸡翁的范围
for (int x = 0; x <= 20; x++) {
// 表示鸡母的范围
for (int y = 0; y <= 33; y++) {
// 表示鸡雏的数量
int z = 100 - x - y;
// 判断表达式z%3==0(因为没有半只鸡卖,所以z对3取余是等于0的)和 5x + 3y + z/3 = 100是否同时成立,如果成立x,y,z就是对应的值
if (z % 3 == 0 && 5 * x + 3 * y + z / 3 == 100) {
System.out.println(x + "," + y + "," + z);
}
}
}
}
}
数组求和
// 数组求和
// 有一个数组,元素是{68,27,95,88,171,996,51,210},求出满足要求的元素和,要求是:求和元素个位和十位不能是7,且只能是偶数
public class ArrayDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};
// 定义计数器
int sum = 0;
// 遍历数组
for (int i = 0; i < arr.length; i++) {
// 判断满足条件就累加
if (arr[i] % 10 != 7 && arr[i] / 10 % 10 != 7 && arr[i] % 2 == 0) {
sum += arr[i];
}
}
System.out.println(sum);
}
}
判断两个数组是否相等
// 编写一个方法判断两个数组是否相等
public class ArrayDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};
int[] arr1 = {68, 27, 95, 88, 171, 996, 51, 210};
// 调用方法
boolean flag = compare(arr, arr1);
System.out.println(flag);
}
public static boolean compare(int[] arr, int[] arr1){
// 首先判断两个数组长度是否相等
if (arr.length != arr1.length){
return false;
}
// 遍历两个数组中的元素是否一样
for(int i = 0; i < arr.length; i++){
if (arr[i] != arr1[i]){
return false;
}
}
return true;
}
}
查找索引
import java.util.Scanner;
// 从键盘录入数据,查找该数据在数组中对应的索引值
public class ArrayDemo {
public static void main(String[] args) {
// 键盘输入数据
Scanner sc = new Scanner(System.in);
// 保存输入的数据
int number = sc.nextInt();
// 定义数组
int[] arr = {1, 2, 3, 4, 5, 6, 7};
int index = getIndex(arr, number);
System.out.println(index);
}
public static int getIndex(int[] arr, int number) {
// 定义变量保存索引,如找不到就返回-1
int index = -1;
// 遍历数组
for (int i = 0; i < arr.length; i++) {
if (number == arr[i]) {
index = i;
}
}
return index;
}
}
数组反转
// 数组的反转
public class ArrayDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// 调用反转的方法
reverse(arr);
// 遍历数组
printArray(arr);
}
public static void reverse(int[] arr) {
// 定义开始索引和结束索引
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
// 交换变量
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
public static void printArray(int[] arr) {
System.out.print("{");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + ",");
}
}
System.out.println("}");
}
}
评委打分
import java.util.Scanner;
// 在编程竞赛中,有6个评委为参赛的选手打分,分数为0-100的整数分,选手最后的得分为:去掉一个最高分和最低分的4个评委平均值(不考虑小数部分)
public class ArrayDemo {
public static void main(String[] args) {
// 定义数组,保存保存分数
int[] arr = new int[6];
// 键盘输入分数
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入分数:");
arr[i] = sc.nextInt();
}
// 调用求最大值方法
int max = getMax(arr);
// 调用求最小值方法
int min = getMin(arr);
// 调用求和方法
int sum = getSum(arr);
// 计算平均值
int avg = (sum - max - min) / (arr.length - 2);
System.out.println(avg);
}
// 定义求数组最大值方法
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
// 定义求数组最小值方法
public static int getMin(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
// 定义数组求和方法
public static int getSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}