|
@@ -0,0 +1,240 @@
|
|
|
+package com.sf.day06;
|
|
|
+
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+public class Test {
|
|
|
+
|
|
|
+ @org.junit.Test
|
|
|
+ public void t1(){
|
|
|
+ int[] num = new int[3];
|
|
|
+ System.out.println(num);
|
|
|
+ System.out.println(num[0]);
|
|
|
+ num[0] = 10;
|
|
|
+ num[1] = 20;
|
|
|
+ num[1] = 90;
|
|
|
+ num[2] = 30;
|
|
|
+// for (int i : num) {
|
|
|
+// System.out.println(i);
|
|
|
+// }
|
|
|
+ for (int i = 0; i < num.length; i++) {
|
|
|
+ System.out.println(num[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * **举例:**创建一个长度为6的int型数组,
|
|
|
+ * 要求数组元素的值都在1-30之间,且是随机赋值。
|
|
|
+ */
|
|
|
+ @org.junit.Test
|
|
|
+ public void t2(){
|
|
|
+ int[] arr = new int[6];
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
+ int num = (int) (Math.random()*30)+1;
|
|
|
+ arr[i] = num;
|
|
|
+// for (int j = 0;j<i;j++){
|
|
|
+// if(arr[i] == arr[j]){
|
|
|
+// i--;
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+ for (int element : arr) {
|
|
|
+ System.out.println(element);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * **举例:**创建一个长度为6的int型数组,
|
|
|
+ * * 要求数组元素的值都在1-30之间,且是随机赋值。
|
|
|
+ */
|
|
|
+ @org.junit.Test
|
|
|
+ public void t3(){
|
|
|
+ int[] arr = new int[6];
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
+ arr[i] = (int)(Math.random()*30)+1;
|
|
|
+ for(int j = 0;j < i;j++){
|
|
|
+ if(arr[i] == arr[j]){
|
|
|
+ i--;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int element : arr) {
|
|
|
+ System.out.println(element);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @org.junit.Test
|
|
|
+ public void t4(){
|
|
|
+ boolean[] flag = new boolean[2];
|
|
|
+ System.out.println(flag[1]);
|
|
|
+ int[] arr = new int[7];
|
|
|
+ System.out.println(arr[4]);
|
|
|
+ Integer[] arr2 = new Integer[7];
|
|
|
+ System.out.println(arr2[4]);
|
|
|
+ }
|
|
|
+
|
|
|
+ @org.junit.Test
|
|
|
+ public void t5(){
|
|
|
+ int[] arr = new int[4];
|
|
|
+ System.out.println(arr[0]);
|
|
|
+ System.out.println(arr[3]);
|
|
|
+// arr = null;
|
|
|
+ System.out.println(arr[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * **练习1:**输出英文星期几
|
|
|
+ *
|
|
|
+ * 用一个数组,保存星期一到星期天的7个英语单词,从键盘输入1-7,显示对应的单词
|
|
|
+ * {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}
|
|
|
+ */
|
|
|
+ @org.junit.Test
|
|
|
+ public void t6(){
|
|
|
+ String[] str = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ while (true){
|
|
|
+ int nextInt = scanner.nextInt();
|
|
|
+ if(nextInt>=1 && nextInt<=7){
|
|
|
+ System.out.println(str[nextInt-1]);
|
|
|
+ System.out.println("还继续吗 Y继续 N退出");
|
|
|
+ if("N".equals(scanner.next())){
|
|
|
+ System.out.println("游戏结束");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ System.out.println("输入有误请重新输入");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用一个数组,保存星期一到星期天的7个英语单词,从键盘输入1-7,显示对应的单词
|
|
|
+ * {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String[] str = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ while (true){
|
|
|
+ System.out.println("请输入周几");
|
|
|
+ int nextInt = scanner.nextInt();
|
|
|
+ if(nextInt>=1 && nextInt<=7){
|
|
|
+ System.out.println(str[nextInt-1]);
|
|
|
+ System.out.println("还继续吗 Y继续 N退出");
|
|
|
+ if("N".equals(scanner.next())){
|
|
|
+ System.out.println("游戏结束");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ System.out.println("输入有误请重新输入");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @org.junit.Test
|
|
|
+ public void t7(){
|
|
|
+ int[][] arr ;
|
|
|
+ arr = new int[2][3]; // 2:二维数组中有2个一维数组 (二维数组的长度) 3:一维数组的长度
|
|
|
+
|
|
|
+ String[][] persons = new String[][]{ {"小舞","唐三","胖子"},{"戴沐白","朱竹清"},{"唐晨","唐浩","唐四","唐五"}};
|
|
|
+ int[][] arr1 = {{3,8,2},{2,7},{9,0,1,6}};
|
|
|
+ System.out.println(arr1);
|
|
|
+ System.out.println(arr1[0]);
|
|
|
+ System.out.println(arr1[0][1]);
|
|
|
+ arr1[0][1] = 88;
|
|
|
+ //遍历
|
|
|
+ for (int i = 0;i< arr1.length;i++){
|
|
|
+ for (int j = 0;j<arr1[i].length;j++){
|
|
|
+ System.out.println(arr1[i][j]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * **练习2:**从键盘读入学生10成绩,找出最高分,并输出学生成绩等级。
|
|
|
+ *
|
|
|
+ * - 成绩>=最高分-10 等级为’A’
|
|
|
+ *
|
|
|
+ * - 成绩>=最高分-20 等级为’B’
|
|
|
+ *
|
|
|
+ * - 成绩>=最高分-30 等级为’C’
|
|
|
+ *
|
|
|
+ * - 其余 等级为’D’
|
|
|
+ *
|
|
|
+ * 提示:先读入学生人数,根据人数创建int数组,存放学生成绩。
|
|
|
+ */
|
|
|
+ @org.junit.Test
|
|
|
+ public void t8(){
|
|
|
+ //1. 根据提示,获取学生人数
|
|
|
+ System.out.print("请输入学生人数:");
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ int count = scanner.nextInt();
|
|
|
+
|
|
|
+ //2. 根据学生人数,创建指定长度的数组 (使用动态初始化)
|
|
|
+ int[] scores = new int[count];
|
|
|
+
|
|
|
+ //3. 使用循环,依次给数组的元素赋值
|
|
|
+ int maxScore = 0; //记录最高分
|
|
|
+ System.out.println("请输入" + count + "个成绩");
|
|
|
+ for (int i = 0; i < scores.length; i++) {
|
|
|
+ scores[i] = scanner.nextInt();
|
|
|
+ //4. 获取数组中元素的最大值,即为最高分
|
|
|
+ if(maxScore < scores[i]){
|
|
|
+ maxScore = scores[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("最高分是:" + maxScore);
|
|
|
+
|
|
|
+ //5. 遍历数组元素,输出各自的分数,并根据其分数与最高分的差值,获取各自的等级
|
|
|
+ char grade;
|
|
|
+ for (int i = 0; i < scores.length; i++) {
|
|
|
+
|
|
|
+ if(scores[i] >= maxScore - 10){
|
|
|
+ grade = 'A';
|
|
|
+ }else if(scores[i] >= maxScore - 20){
|
|
|
+ grade = 'B';
|
|
|
+ }else if(scores[i] >= maxScore - 30){
|
|
|
+ grade = 'C';
|
|
|
+ }else{
|
|
|
+ grade = 'D';
|
|
|
+ }
|
|
|
+ System.out.println("student " + i + " socre is " +
|
|
|
+ scores[i] + ", grade is " + grade);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @org.junit.Test
|
|
|
+ public void t9(){
|
|
|
+ /**
|
|
|
+ * 商城,
|
|
|
+ *
|
|
|
+ * 第一季度 每一个月的销售额 分别是 12,14,24,
|
|
|
+ *
|
|
|
+ * 第二季度 每一个月的销售额 分别是 23,18,35,
|
|
|
+ *
|
|
|
+ * 第三季度 每一个月的销售额 分别是 33,48,25,
|
|
|
+ *
|
|
|
+ * 第四季度 每一个月的销售额 分别是 27,28,35,
|
|
|
+ *
|
|
|
+ * 二维数组实现
|
|
|
+ *
|
|
|
+ * 求出 每个月的平均销售额,和总销售额。
|
|
|
+ */
|
|
|
+ int[][] aa={{12,14,24},{23,18,35},{33,48,25},{27,28,35}};
|
|
|
+ int sum = 0;
|
|
|
+ int count = 0;
|
|
|
+ for (int i = 0;i< aa.length;i++){
|
|
|
+ for (int j = 0;j<aa[i].length;j++){
|
|
|
+ count++;
|
|
|
+ sum += aa[i][j];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(sum);
|
|
|
+ System.out.println(count);
|
|
|
+ System.out.println(sum/count);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|