Main
package edu.xcdq;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int [] scores = new int[5] ; // 声明数组,并开辟空间
int sum = 0 ;
//赋值
Scanner scanner = new Scanner(System.in);
for (int i =0; i<5 ;i++){
System.out.println("请输入第" +(i+1) + "学生的成绩" );
scores[i] = scanner.nextInt() ;
sum += scores[i]; //把数组中i位置元素累加到sum中
}
System.out.println("平均分:" + sum/ scores.length);
}
}
demo01
import java.util.Scanner;
/**
* @qvthor liuwenzheng
* @date 2021/3/24 8:26
*/
public class demo01 {
public static void main(String[] args) {
int []scores = {8,4,2,1,23,344,12};
int sum = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 0 ; i<scores.length ;i++){
System.out.printf( scores[i] +"\t" );
sum += scores[i];
}
System.out.println("数列所有值的和为:" + sum);
boolean flag = false ;
int taget = scanner.nextInt();
for (int a = 0 ; a< scores.length; a++){
if (scores[a] ==taget){
flag=true;
System.out.println("包含该元素");
}
}
if (!flag){
System.out.println("不包含该元素" +
"");
}
}
}
demo02排序
import java.util.Scanner;
/**
* @qvthor liuwenzheng
* @date 2021/3/24 9:13
*/
public class demo02排序 {
public static void main(String[] args) {
int [] scores = new int[5];
Scanner scanner = new Scanner(System.in);
for (int i = 0 ;i <scores.length; i++){
System.out.println("请输入" + (i+1)+ "个数");
scores[i] = scanner.nextInt();
}
//排序
for (int i = 0 ; i< scores.length; i++){
System.out.println(scores[i] + "\t");
}
}
}
demo03计算最大值
/**
* @qvthor liuwenzheng
* @date 2021/3/24 9:19
*/
public class demo03计算大值 {
// 求最大值 打擂台
public static void main(String[] args) {
int [] scores = new int[] {68,45,99,58};
int max = scores[0]; //默认第一个数组元素为最大值
for (int i =0 ; i < scores.length;i++){
if (scores[i] >max){
max = scores[i]; //如果当前元素的值比max中的值大,用当前值覆盖max中的值
}
}
System.out.println("最大值为" + max);
}
}
demo04
import java.util.Arrays;
import java.util.Scanner;
/**
* @qvthor liuwenzheng
* @date 2021/3/24 9:40
*/
public class demo04 {
public static void main(String[] args) {
//1 接受用户的输入
int [] scores = new int[6] ; //在创建的时候就给定了默认值,为0
Scanner scanner = new Scanner(System.in);
for (int i = 0 ; i <scores.length-1 ; i++ ){ //留出最后一个空间
System.out.println("请输入第一个" + (i+1) + "个值");
scores [i] = scanner.nextInt();
}
/*
for (int i = 0 ; i <scores.length; i++) ;
System.out.println(scores[i] + "\t");
}
*/
//2排序
Arrays.sort(scores);
//3 接受用户要插入的值
System.out.println("请输入你要插入的值");
int value = scanner.nextInt();
//4 找到插入位置
//4.1 找到第一个大于value的位置
int index = 0 ; //用于记录第一个大于value的位置
for (int i = 0 ; i<scores.length; i++){
if (value > scores[i]){
index = i ;
break;
}
}
// 4.2 把这个位置及后面的元素向后移,空出一个位置不需要移动
for (int i = scores.length-2 ;i <=index ; i--){ //最后一个位置不需要移动
scores[i+1] = scores[i]; // 元素后移
}
//4.3 将要插入的元素放在正确位置
scores[index] = value;
//5 打印插入后的结果
for (int i = 0 ;i< scores.length;i++){
System.out.println(scores[i] + "\t");
}
}
}
demo05
import java.util.Arrays;
import java.util.Scanner;
/**
* @qvthor liuwenzheng
* @date 2021/3/24 10:44
*/
public class demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); String [] scores = new String[]{"Nike背包,Adidas运动衫,Kappa外套,361°腰包);"};
System.out.println(scores); //打印数组的起始地址
double sum = 0;
System.out.println("请输入会员本月的消费记录");
for (int i = 0 ; i< scores.length; i++){
System.out.println(scores[i]);
}
}
}
demo06
import java.util.Scanner;
/**
* @qvthor liuwenzheng
* @date 2021/3/24 11:37
*/
public class demo06 {
public static void main(String[] args) {
double [] money = new double[5];
Scanner scanner = new Scanner(System.in);
int sum = 0 ;
for (int i= 0 ; i < money.length; i++){
System.out.println("请输入第" + (i+1) + "笔金额");
money[i] =scanner.nextDouble();
}
}
}