From 12ecc21fd50a8593a159d5123ade0d55065c17b7 Mon Sep 17 00:00:00 2001 From: pj3q7x5mz <1732875197@qq.com> Date: Thu, 17 Nov 2022 11:22:27 +0800 Subject: [PATCH] ADD file via upload --- Java入门 - 数组基础.txt | 138 ++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Java入门 - 数组基础.txt diff --git a/Java入门 - 数组基础.txt b/Java入门 - 数组基础.txt new file mode 100644 index 0000000..f002cb7 --- /dev/null +++ b/Java入门 - 数组基础.txt @@ -0,0 +1,138 @@ +1.初识数组 +package step1; + +public class HelloWorld { + public static void main(String[] args) { + /********** Begin **********/ + int[] scores = {91,88,60}; + + System.out.println("数组的第一个值为:"+scores[0]); //在这里输出数组的第一个值 + System.out.println("数组的第二个值为:"+scores[1]); //在这里输出数组的第二个值 + System.out.println("数组的第三个值为:"+scores[2]); //在这里输出数组的第三个值 + /********** End **********/ + } +} +2.数组的使用 +package step2; + +import java.util.Scanner; + +public class HelloWorld { + public static void main(String[] args) { + + + /********** Begin **********/ + //在这里定义一个长度为4的字符串数组,用来存放学生姓名 + String[] stuNames = new String[4]; + + //在这里给stuNames数组赋值 分别为 张三,张无忌,张三丰,张岁山 + stuNames[0] = "张三"; + stuNames[1] = "张无忌"; + stuNames[2] = "张三丰"; + stuNames[3] = "张岁山"; + + + //在这里输出stuNames数组中的数据 + System.out.println("数组中的第一个数据为:" +stuNames[0] ); + System.out.println("数组中的第二个数据为:" + stuNames[1]); + System.out.println("数组中的第三个数据为:" + stuNames[2]); + System.out.println("数组中的第四个数据为:" + stuNames[3]); + + + int[] scores; + Scanner sc = new Scanner(System.in); + //在这里使用Scanner获取系统输入的整数,并用获取到的数据来设置scores数组的长度 + int length = sc.nextInt(); + scores = new int[length]; + /********** End **********/ + + System.out.println("数组scores的长度为:" + scores.length); + } +} +3.选择题 +double[] num=new double[]; +程序出错了 +ABCD +4.数组练习-平均值和最大值 +package step3; + +import java.util.Scanner; + +public class HelloWorld { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int[] scores = new int[sc.nextInt()]; + + //循环给数组赋值 + for(int i = 0 ; i< scores.length;i++){ + scores[i] = sc.nextInt(); + } + /********** Begin **********/ + //在这里计算数组scores的平均值和最大值 + float sum = 0; + int max = scores[0]; + float avg; + for(int i = 0; i < scores.length; i++){ + sum = sum + scores[i]; + + } + for(int i = 1; i < scores.length; i++){ + if(scores[i]>scores[i-1]){ + max = scores[i]; + }else{ + break; + } + + } + + + avg = sum / scores.length; + + + + + + System.out.println("平均值:"+avg); + System.out.println("最大值:"+max); + /********** End **********/ + } +} +5.二维数组 +package step4; + +public class HelloWorld { + public static void main(String[] args) { + /********** Begin **********/ + int[][] scores = {{92,85},{91,65},{90,33}}; + + for(int i=0; i