1.输入5人成绩求总分和平均分
import java.util.Scanner;
public class MyDemo036 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int [] score = new int[5];
for ( int i=0; i<5 ; i++ ) {
System.out.println("请输入第"+(i+1)+"个学员成绩");
score[i]=input.nextInt();
}
//计算总成绩和平均成绩
int count=0;
for ( int i=0; i<5 ; i++ ) {
count+=score[i];
}
System.out.println("5人总成绩:"+count);
System.out.println("5人平均成绩:"+count/5.0);
}
}
运行结果如下:
2.判断一个数是否存在于数组中
import java.util.Scanner;
public class MyDemo037 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int [] arr = {8,4,2,1,23,344,12};
System.out.println("请输入一个数:");
int num = input.nextInt();
//定义变量记录数据是否存在
boolean has=false;
for ( int i=0 ; i < arr.length ; i++) {
if( num==arr[i] ) {
has=true;
break;
}
}
System.out.println(has?"存在":"不存在");
}
}
运行结果如下: