①: 计算4个学生的总分,平均分,最高分,最低分。
package Z1;
public class Test01 {
public static void main(String[] args) {
int a[] = { 95, 85, 75, 65 };
double sum;
sum = a[0] + a[1] + a[2] + a[3];
System.out.println("总分" + sum);
double avg;
avg = (a[0] + a[1] + a[2] + a[3]) / 4;
System.out.println("平均分" + avg);
int i;
int max = a[0];
for (i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
}
System.out.println("最高分" + max);
int min = a[0];
for (i = 0; i < a.length; i++) {
if (min > a[i]) {
min = a[i];
}
}
System.out.println("最低分" + min);
}
}
②:判断一个五位数属否为回文数
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i;
int[] a = new int[5];
System.out.println("请输入一个五位整数,用于判断是否为回文数:");
int b = input.nextInt();
for(i=0;i<a.length;i++){
a[i] = b % 10;
b /= 10;
}
for(i=0;i<(a.length/2);i++){
if (a[i]!=a[a.length-i-1]){
System.out.println("该数不是回文数。");
break;
}else if(i==a.length/2-1){
System.out.println("该数是回文数。");
}
}
③:查找在数组里是否有该字符
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name[] = { "ZR", "ZM", "ZMK" };
System.out.println("请输入学员姓名");
String search = sc.next();
for (int i = 0; i < name.length; i++) {
if (search.equals(name[i])) {
System.out.println("学员存在");
break;
} else if(i==name.length-1){
System.out.println("查无此人");
}
}
}
}