|
@@ -0,0 +1,237 @@
|
|
|
|
+package com.four.day06;
|
|
|
|
+
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.Random;
|
|
|
|
+import java.util.Scanner;
|
|
|
|
+
|
|
|
|
+public class DemoMethod01 {
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+// double 头等舱 = calculate(101, "qq", 1000);
|
|
|
|
+// System.out.println(头等舱);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// String code = createCode(4);
|
|
|
|
+// System.out.println(code);
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// System.out.println((int)'a');
|
|
|
|
+// System.out.println((int)'z');
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// double averageScore = getAverageScore(4);
|
|
|
|
+// System.out.println(averageScore);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// int encrypt = encrypt(1983);
|
|
|
|
+// System.out.println(encrypt);
|
|
|
|
+
|
|
|
|
+ int[] arr = {11,22,33};
|
|
|
|
+ System.out.println(arr);
|
|
|
|
+ int[] ints = copyArr(arr);
|
|
|
|
+ System.out.println(ints);
|
|
|
|
+
|
|
|
|
+ //源码
|
|
|
|
+ int[] ints1 = Arrays.copyOf(arr,2);
|
|
|
|
+ System.out.println(ints1);
|
|
|
|
+
|
|
|
|
+ int[] arr2 = {11,22,33,44,55};
|
|
|
|
+ System.out.println(arr2);
|
|
|
|
+ int[] ints2 = copyArr2(arr2);
|
|
|
|
+ System.out.println(ints2);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ src – the source array.
|
|
|
|
+ srcPos – starting position in the source array.
|
|
|
|
+ dest – the destination array.
|
|
|
|
+ destPos – starting position in the destination data.
|
|
|
|
+ length – the number of array elements to be copied.
|
|
|
|
+ System.arraycopy(original, 0, copy, 0,
|
|
|
|
+ Math.min(original.length, newLength));
|
|
|
|
+
|
|
|
|
+ //nactive 本地方法 底层 用c 编写。
|
|
|
|
+ public static native void arraycopy(Object src, int srcPos,
|
|
|
|
+ Object dest, int destPos,
|
|
|
|
+ int length);
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ /*
|
|
|
|
+ * - 参数 原价、月份、舱位类型
|
|
|
|
+ * - 返回值 :机票价格,类型double
|
|
|
|
+ * - 业务逻辑
|
|
|
|
+ * 5-10 头 9 经济 8.5
|
|
|
|
+ * 11-4 头 7 经济 6.5
|
|
|
|
+ * 根据 月份判断 根据船舱类型
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ public static double calculate( int month , String type , int price ) {
|
|
|
|
+ //健壮性判断
|
|
|
|
+ //月份判断
|
|
|
|
+ if ( month < 1 || month > 12 ){
|
|
|
|
+ System.out.println("月份不符合规范");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //单或者
|
|
|
|
+ if ( !type.equals("头等舱") && !type.equals("经济舱") ){
|
|
|
|
+ System.out.println("类型不符合规范");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //折扣价
|
|
|
|
+ double resultPrice = -1;
|
|
|
|
+
|
|
|
|
+ //判断类型
|
|
|
|
+ switch ( type ){
|
|
|
|
+ case "头等舱":
|
|
|
|
+ if ( month >= 5 && month <= 10 ){
|
|
|
|
+ resultPrice = price * 0.9;
|
|
|
|
+ }else{
|
|
|
|
+ resultPrice = price * 0.8;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case "经济舱":
|
|
|
|
+ if ( month >= 5 && month <= 10 ){
|
|
|
|
+ resultPrice = price * 0.85;
|
|
|
|
+ }else{
|
|
|
|
+ resultPrice = price * 0.65;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ return resultPrice;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String createCode( int num ){
|
|
|
|
+ //数字 字母 大小写
|
|
|
|
+
|
|
|
|
+ //循环
|
|
|
|
+ //随机 数字 0 大字母 1 小写字母 2
|
|
|
|
+ //数字 0-9
|
|
|
|
+ //大字母 A-Z 65 + 26
|
|
|
|
+ //小写字母 a-z 97+26
|
|
|
|
+ Random r = new Random();
|
|
|
|
+ String str = "";
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < num ; i++) {
|
|
|
|
+ //随机数
|
|
|
|
+ int number = r.nextInt(3);
|
|
|
|
+ //switch
|
|
|
|
+ switch (number){
|
|
|
|
+ case 0:
|
|
|
|
+ str += r.nextInt(10);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 1:
|
|
|
|
+ str += (char) (r.nextInt(26) + 65);
|
|
|
|
+ break;
|
|
|
|
+ case 2:
|
|
|
|
+ str += (char) (r.nextInt(26) + 97);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return str;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static double getAverageScore( int n ){
|
|
|
|
+ Scanner sc = new Scanner(System.in);
|
|
|
|
+ //健壮性
|
|
|
|
+ //数组的长度 = n
|
|
|
|
+ int[] arr = new int[n];
|
|
|
|
+ //赋值
|
|
|
|
+ for (int i = 0; i < n ; i++) {
|
|
|
|
+ System.out.println("请输入0-100之间的数字");
|
|
|
|
+ int number = sc.nextInt();
|
|
|
|
+ if ( number < 0 || number > 100 ){
|
|
|
|
+ System.out.println("输入的不符号规范,请重新输入");
|
|
|
|
+ i--;
|
|
|
|
+ }
|
|
|
|
+ //赋值
|
|
|
|
+ arr[i] = number;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取数组中的最大值和最小值
|
|
|
|
+ //最大值 最小值 和
|
|
|
|
+ int max = arr[0];
|
|
|
|
+ int min = arr[0];
|
|
|
|
+ int sum = 0;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ if ( arr[i] > max ){
|
|
|
|
+ max = arr[i];
|
|
|
|
+ }
|
|
|
|
+ if ( arr[i] < min ){
|
|
|
|
+ min = arr[i];
|
|
|
|
+ }
|
|
|
|
+ sum += arr[i];
|
|
|
|
+ }
|
|
|
|
+ return (sum - max -min) / (n-2);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int encrypt(int n){
|
|
|
|
+ if ( n <1000 || n >= 10000 ){
|
|
|
|
+ System.out.println("数字不符合四位数");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ //数组
|
|
|
|
+ int[] arr = new int[4];
|
|
|
|
+ arr[0] = n/1000;
|
|
|
|
+ arr[1] = n /100 % 10;
|
|
|
|
+ arr[2] = n /10 % 10;
|
|
|
|
+ arr[3] = n % 10;
|
|
|
|
+
|
|
|
|
+ //+ 5 取余数
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ arr[i] += 5;
|
|
|
|
+ //余数
|
|
|
|
+ arr[i] %= 10;
|
|
|
|
+ }
|
|
|
|
+ //反转
|
|
|
|
+ for (int i = 0,j= arr.length-1; i < j ; i++,j--) {
|
|
|
|
+ int temp = arr[i];
|
|
|
|
+ arr[i] = arr[j];
|
|
|
|
+ arr[j] = temp;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String str = "";
|
|
|
|
+ //获取每一位数字
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ str+=arr[i];
|
|
|
|
+ }
|
|
|
|
+ //转换
|
|
|
|
+ return Integer.parseInt(str);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static int[] copyArr(int[] source ){
|
|
|
|
+ //健壮性
|
|
|
|
+ // null 长度
|
|
|
|
+ //创建数组长度
|
|
|
|
+ int[] dest = new int[source.length];
|
|
|
|
+
|
|
|
|
+ //赋值内容
|
|
|
|
+ for (int i = 0; i < source.length; i++) {
|
|
|
|
+ dest[i] = source[i];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return dest;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int[] copyArr2(int[] source ){
|
|
|
|
+ //健壮性
|
|
|
|
+ // null 长度
|
|
|
|
+ //创建数组长度
|
|
|
|
+ int[] dest = new int[source.length];
|
|
|
|
+ //复制数组
|
|
|
|
+ System.arraycopy(source,0,dest,0,source.length);
|
|
|
|
+
|
|
|
|
+ return dest;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|