打分案例
package com.lzk.test;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
//评委打分案例,输入五位评委打分,去掉最高分和最低分,求平均分
Scanner scanner = new Scanner(System.in);
System.out.println("请输入五位评委打分,用空格分隔:");
String input = scanner.nextLine();
String[] scores = input.split(" ");
int sum = 0;
int max = Integer.MIN_VALUE;//转成int
int min = Integer.MAX_VALUE;
for (int i = 0; i < scores.length; i++) {
int score = Integer.parseInt(scores[i]);
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
sum += score;
}
System.out.println("去掉最高分和最低分后,平均分为:" + 1.0* (sum - max - min) / (scores.length - 2));//1.0可以计算出小数
}
}
加密
package com.lzk.test;
import java.util.Scanner;
public class test {
//对四个数字进行加密处理,,首先+5,然后%10,然后反转,最后输出结果
public static void main(String[] args) {
//接受一个千位数
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个四位数:");//1983
int number = scanner.nextInt();
encrypt(number);
}
//将这个千位数存放在数组,遍历数组,对每个数字进行加密处理
public static void encrypt(int number) {
int[] numbers = split(number);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (numbers[i] + 5) % 10;
System.out.println("number[" + i +"] = " + numbers[i]);//8346
}
reverse(numbers);
String result = "";
for (int i = numbers.length - 1; i >= 0; i--) {
result += numbers[i];
}
System.out.println("加密结果:" + result);//8346
}
//将一个四位数分割成数组
public static int[] split(int number) {
int[] numbers = new int[4];
for (int i = 0; i < 4; i++) {
numbers[i] = number % 10;
number /= 10;
System.out.println("numbers[" + i +"] = " + numbers[i]);//3891
}
return numbers;
}
public static void reverse(int[] numbers) {
int temp;
for (int i = 0, j = numbers.length - 1; i < j; i++, j--) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
拷贝数组
package com.lzk.test;
import java.util.Scanner;
public class test {
//拷贝数组
public static void copyArray(int[] arr){
int[] newArr = new int[arr.length];
for(int i=0;i<arr.length;i++){
newArr[i] = arr[i];
}
System.out.println("拷贝后的数组为:");
for(int i=0;i<newArr.length;i++){
System.out.print(newArr[i]+" ");
}
}
//不能用System.out.println()输出数组,因为数组元素之间有空格,会导致输出不正常
//int[] newArr = arr,因为这只是把地址指向了,并没有拷贝数组元素
//输入数组
public static void inputArray(int[] arr){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数组元素:");
for(int i=0;i<arr.length;i++){
arr[i] = scanner.nextInt();
}
System.out.println("输入的数组为:");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}