基础知识--方法,数组等

加密计算器

 package com.Leo.struct;
 
 import java.util.Scanner;
 
 public class calculate {
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
         System.out.println("请输入密码:");
         int i = 3;
         while (i>0) {
             String passWord = scanner.nextLine();
             if (passWord.equals("1qaz2wsxC")) {
                 System.out.println("密码正确,欢迎使用");
                 System.out.println("请选择功能:");
                 functionChoose(scanner.nextInt());
            } else {
                 System.out.println("密码错误,请重新输入");
            }
             i--;
        }
         scanner.close();
    }
     public static void functionChoose(int i){
         switch (i){
             case 1:
             myadd();
             break;
 
             case 2:
                 //mydid();
                 break;
        }
    }
     public static void myadd(){
         double sum = 0;
         double numOfNum = 0;
         System.out.println("请输入数字,每个数字以回车结束,输入其他字符退出功能:");
         Scanner scanner = new Scanner(System.in);
         while (scanner.hasNextDouble()){
             double num = scanner.nextDouble();
             numOfNum++;
             sum += num;
             System.out.println("当前"+numOfNum+"个输入的数字和为"+sum);
        }
         System.out.println("总和为"+sum);
         scanner.close();
    }
 }

 

方法重载

当存在相同的函数名,但形式参数不同的函数:

 package com.Leo.method;
 
 
 public class demo01 {
     public static void main(String[] args) {
         //修饰符 返回值类型 方法名(参数类型 参数名){方法体 return 返回值}
         int max = max(1,2);
         int max2 = max(1,4,5);
         System.out.println(max);
         System.out.println(max2);
    }
     public static int max(int a, int b){
         int result = a;
         if (a<b){
             result = b;
        }
         return result;
    }
     public static int max(int a,int b,int c){
         int result = a;
         if(a<b){
             result = b;
        }
         if (result<c){
             result = c;
        }
         return result;
    }
 }

例如上例中,两个比较大小的方法max(),且返回值均为Int类型,但实际上,max调用的是第一个函数,max2调用的是第二个函数;


规则

方法名必须相同

参数列表必须不同(个数不同、类型不同、或参数排列顺序不同等)

方法的返回类型可以相同也可以不同

仅仅返回类型不同不足以成为方法的重载


可变参数

JAVA支持传递同类型的可变参数给一个方法:


在方法的声明中,给指定的参数类型后边加一个省略号

一个方法仅可指定一个可变参数,且必须是最后一个参数


递归

即自己调用自己;

能不用就不用递归

加密计算器改进

 package com.Leo.method;
 
 import java.util.Scanner;
 
 public class MyCalculateTry2 {
 
     public static void main(String[] args) {
         MyCalculateTest myCalculateTest = new MyCalculateTest();
         Scanner scanner = new Scanner(System.in);
         boolean flag = true;
         boolean flag2 = true;
         int time = 1;
         System.out.println("欢迎使用计算器");
         while (flag2){
             System.out.println("请输入密码:");
             String pw = scanner.nextLine();
             if (!pw.equals("1qaz2wsxC")){
                 System.out.println("输入错误");
            }else {
                 flag2 = false;
            }
        }
         while (flag){
             System.out.println("这是您第"+time+"次使用计算器");
             if (time>1){
                 System.out.println("是否继续使用计算器?");
                 String choice = scanner.next();
                 if (choice.equals("n")){//注意是"n",不是单引号'n'
                     flag = false;
                     break;
                }
            }
             System.out.println("请输入第一个数字");
             double num1 = scanner.nextDouble();
             System.out.println("请输入所使用的计算方法:(+,-,*,/)");
             String cal = scanner.next();
             System.out.println("请输入第二个数字");
             double num2 = scanner.nextDouble();
             double result = 0;
             switch (cal){
                 case "+":
                     result = num1 + num2;
                     System.out.println(num1+"+"+num2+"="+result);
                     break;
 
                 case "-":
                     result = num1 - num2;
                     System.out.println(num1+"-"+num2+"="+result);
                     break;
 
                 case "*":
                     result = num1 * num2;
                     System.out.println(num1+"*"+num2+"="+result);
                     break;
 
                 case "/":
                     result = num1 / num2;
                     System.out.println(num1+"/"+num2+"="+result);
                     break;
 
                 default:
                     System.out.println("输入有误");
                     break;
            }
             time++;
        }
         scanner.close();
    }
 }

数组

长度确定,一旦被创建,数组的大小就不可以改变

数组中的元素必须是相同类型的,不允许出现混合类型

数组中的元素可以是任何数据类型,包括基本类型和引用类型

数组变量属于应用类型,数组也可以看成是对象,数组中的每一个元素相当于改对象的成员变量。数组本身就是对象,JAVA中对象是在堆种的,因此数组无论保存原始类型还是其他对象类型,数组对象本省是在堆中的


稀疏数组

压缩

应用场景:

当一个数组中大部分元素为同一个值(例如0)时,可以用稀疏数组来保存;

稀疏数组会记录数组的行数和列数,并记录有多少个不同的值;

例如

0 1 1 0 0 0

2 3 0 0 0 0

则稀疏数组为


行 列 值

2 6 4

0 1 1

0 2 1

1 0 2

1 1 3


通过坐标来记录每个值的位置及值,可以起到压缩空间的效果,提升效率

 

代码实操

 package com.Leo.array;
 
 public class ArrayDemo07 {
     public static void main(String[] args) {
         //创建一个二维数组 11*11 0:无棋子;1:黑旗;2:白旗
         int[][] array1 = new int[11][11];
         array1[1][2] = 1;
         array1[2][3] = 2;
         System.out.println("输出当前的棋子位置");
         for (int[] ints:array1
              ) {
             for (int anInt:ints
                  ) {
                 System.out.print(anInt+"\t");
            }
             System.out.println();
        }
         //转换为稀疏数组保存
         //获取有效值的个数
         int sum = 0;
         for (int i = 0; i < 11; i++) {
             for (int i1 = 0; i1 < 11; i1++) {
                 if (array1[i][i1] != 0){
                     sum++;
                }
            }
        }
         System.out.println("有效值的个数:"+sum);
         //创建一个稀疏数组的数组
         int[][] array2 = new int[sum+1][3];
         array2[0][0] = 11;
         array2[0][1] = 11;
         array2[0][2] = sum;
         //遍历二维数组,将非零的值坐标及值存在稀疏数组中
         int xIndex = 1;
         int yIndex = 1;
         for (int i = 0; i < array1.length; i++) {
             for (int i1 = 0; i1 < array1[i].length; i1++) {
                 if (array1[i][i1]!=0) {
                     array2[xIndex][0] = i;
                     array2[xIndex][1] = i1;
                     array2[xIndex][2] = array1[i][i1];
                     xIndex++;
                }
            }
        }
         System.out.println("输出稀疏数组");
         for (int i = 0; i < array2.length; i++) {
             for (int i1 = 0; i1 < array2[i].length; i1++) {
                 System.out.print(array2[i][i1]+"\t");
            }
             System.out.println();
        }
         System.out.println("还原");
         //还原数组
         int[][] array3 = new int[array2[0][0]][array2[0][1]];
         for (int i = 1; i < array2.length; i++) {
 
                 array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
         for (int i = 0; i < array3.length; i++) {
             for (int i1 = 0; i1 < array3[i].length; i1++) {
                 System.out.print(array3[i][i1]+"\t");
            }
             System.out.println();
        }
    }
 }

结果:


输出当前的棋子位置 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 有效值的个数:2 输出稀疏数组 11 11 2 1 2 1 2 3 2 还原 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000000000000

Process finished with exit code 0


posted @ 2021-07-07 23:27  LeoChen'blog  阅读(40)  评论(0)    收藏  举报