第二周-Java基础

2022-02-28

ForDemo类

package day0228;
/**
 * for循环
 * 语法for(变量初始化;条件;自增变量){代码块}
 * 注意:1.变量初始化,只进行一次
 * 		2.每次循环前进行条件判断,条件满足执行代码
 * 		3.每次代码执行完,执行自增表达式
 * 两种循环里面的控制中断的方式:
 * break;跳出循环
 * continue:跳出当前循环,continue后面的语句不在执行,进入下一次循环
 * @author Katrina
 */
public class ForDemo {

	public static void main(String[] args) {
		for (int x = 1; x <= 10; x++) {
			if (x == 5) {
//				break; //1 2 3 4 跳出循环
				continue; //1 2 3 4 6 7 8 9 10 没有5 跳出了当前循环
			}
			//执行代码块
			System.out.print(x + " "); //1 2 3 4 5 6 7 8 9 10 
		}
	}
	
}

ForDemo2类

package day0228;
/**
 * 循环嵌套:从外层循环开始,初始化变量,进行比较,
 * 满足条件执行外部循环的代码块
 * 内部循环开始执行,初始化变量,进行比较,
 * 满足条件执行内部循环的代码块:一直执行到内部循环条件不满足,退出
 * 此时,外部循环进行自增表达式执行及条件比较。
 * 重复以上过程。
 * 1.从外部切入
 * 2.外部一个变量的值,对应的内部循环全部次数执行完毕
 * 3.外部不断循环
 * 4.外部循环退出
 * @author Katrina
 */
public class ForDemo2 {
	
	public static void main(String[] args) {
		/*
		 1 + 1 = 2 1 + 2 = 3 1 + 3 = 4 1 + 4 = 5 1 + 5 = 6 1 + 6 = 7 1 + 7 = 8 1 + 8 = 9 1 + 9 = 10 
		 2 + 1 = 3 2 + 2 = 4 2 + 3 = 5 2 + 4 = 6 2 + 5 = 7 2 + 6 = 8 2 + 7 = 9 2 + 8 = 10 2 + 9 = 11 
		 3 + 1 = 4 3 + 2 = 5 3 + 3 = 6 3 + 4 = 7 3 + 5 = 8 3 + 6 = 9 3 + 7 = 10 3 + 8 = 11 3 + 9 = 12 
		 4 + 1 = 5 4 + 2 = 6 4 + 3 = 7 4 + 4 = 8 4 + 5 = 9 4 + 6 = 10 4 + 7 = 11 4 + 8 = 12 4 + 9 = 13 
		 5 + 1 = 6 5 + 2 = 7 5 + 3 = 8 5 + 4 = 9 5 + 5 = 10 5 + 6 = 11 5 + 7 = 12 5 + 8 = 13 5 + 9 = 14 
		 6 + 1 = 7 6 + 2 = 8 6 + 3 = 9 6 + 4 = 10 6 + 5 = 11 6 + 6 = 12 6 + 7 = 13 6 + 8 = 14 6 + 9 = 15 
		 7 + 1 = 8 7 + 2 = 9 7 + 3 = 10 7 + 4 = 11 7 + 5 = 12 7 + 6 = 13 7 + 7 = 14 7 + 8 = 15 7 + 9 = 16 
		 8 + 1 = 9 8 + 2 = 10 8 + 3 = 11 8 + 4 = 12 8 + 5 = 13 8 + 6 = 14 8 + 7 = 15 8 + 8 = 16 8 + 9 = 17 
		 9 + 1 = 10 9 + 2 = 11 9 + 3 = 12 9 + 4 = 13 9 + 5 = 14 9 + 6 = 15 9 + 7 = 16 9 + 8 = 17 9 + 9 = 18 
		 10 + 1 = 11 10 + 2 = 12 10 + 3 = 13 10 + 4 = 14 10 + 5 = 15 10 + 6 = 16 10 + 7 = 17 10 + 8 = 18 10 + 9 = 19 
		 */
//		for (int i = 1; i <= 10; i++) {
//			for (int j = 1; j < 10; j++) {
//			//						                                    连接符  加法			
//				System.out.print(i + " + " + j + " = " + (i + j) + " ");
//			}
//			System.out.println();
//		}
		
		/*
		 1 * 1 = 1 
		 2 * 1 = 2 2 * 2 = 4 
		 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 
		 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 
		 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 
		 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 
		 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 
		 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 
		 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 
		 */
//		for (int i = 1; i < 10; i++) {
//			for (int j = 1; j <= i; j++) {
//				System.out.print(i + " * " + j + " = " + (i * j) + " ");
//			}
//			System.out.println();
//		}
		
		/*
		  *
		  **
		  ***
		*/
//		for (int i = 1; i <= 3; i++) { //控制行
//			for (int j = 1; j <= i; j++) { //控制列
//				System.out.print("*");
//			}
//			System.out.println();
//		}
		
		/*
		   *
		  * * 
         * * * 
		*/
		for (int i = 1; i <= 3; i++) { 
			for (int j = 2; j >= i; j--) {
				System.out.print("+");
				
			 }
			for (int k = 1; k <= i; k++) {
				System.out.print("* ");
			}
			System.out.println();
		}
	}
	
}

WhileDemo类

package day0228;
/**
 * while循环
 * 语法:while(条件表达式){执行语句块}
 * 注意:条件表达式是boolean型
 * @author Katrina
 */
public class WhileDemo {

	public static void main(String[] args) {
		int x = 1;
//		while (x < 3) {
//			System.out.print(x + " "); //1 2
//			x++;
//		}
//		while (++x < 3) {
//			System.out.print(x + " "); //2
//		}
		while (x++ < 3) { //1和3先比较,再比较2和3
			System.out.print(x + " "); //2 3
		}
	}
	
}

DoWhileDemo类

package day0228;

/**
 * do...while循环
 * 语法:do{执行语句块} while{条件表达式};
 * 与while不同之处:1.do...while()执行代码块必定至少执行一次,while()循环可能一次也不执行
 * 				   2.do...while()括号后面必须有;
 * @author Katrina
 */
public class DoWhileDemo {

	public static void main(String[] args) {
		int x = 1;
//		do {
//			System.out.print(x + " "); //1 2 3
//		} while (x++ < 3);
		
		/*不同*/
		while (x > 3) {
			System.out.println(x);
		}
		System.out.println("循环结束");
		
		do {
			System.out.println(x); //1
		} while (x > 3);
		System.out.println("循环结束");
	}
	
}

Exercise类

package day0228;
public class Exercise {

	public static void main(String[] args) {
//		for (int i = 10; i >= 1 ; i--) {
//			System.out.println(i);
//		}
		
		int x = 11;
//		while (x >= 1) {
//			System.out.println(x);
//			x--;
//		}
		
//		do {
//			System.out.println(x);
//			x--;
//		} while (x >= 1)
		
		//跳过6
		while (x >= 2) {
			x--; //必须放在if前面,因为判断x==6之后跳出循环,x--未执行
			if (x == 6) {
				continue;
			}
			System.out.println(x);
		}
		//到6结束
//		do {
//			if (x == 5) {
//				break;
//			}
//			System.out.println(x);
//			x--;
//		} while (x >= 1);
	}
	
}

Homework类

package day0228;
public class Homework {

	public static void main(String[] args) {
		/*
		   *
		  ***
		 *****
		  ***
		   *
		 */
		for (int i = 1; i <= 3; i++) {
			for (int j =1; j <= 3 - i; j++) {
				 System.out.print(" "); 
			}
			for (int k =1; k <= 2 * i - 1; k++) {
				 System.out.print("*"); 
			}
			System.out.println();
	    }
	    for (int i = 1; i <= 2; i++) {
		    for (int j = 1; j <= i; j++) {
			   System.out.print(" ");
		    }
		    for(int n = 1; n <= 2 * (2 - i) + 1; n++) {
			   System.out.print("*");
		    }
		    System.out.println();
	   }
		
		/*
		153是水仙花数
		370是水仙花数
		371是水仙花数
		407是水仙花数
		1000是水仙花数        
		 */
//		for (int i = 100; i <= 1000; i++) {
//			int h = i / 100;
//			int t = i % 100/ 10;
//			int g = i % 10 ;
//			if (h*h*h + t*t*t + g*g*g == i) {
//				System.out.println(i + "是水仙花数");
//			}
//		}
	}
}

2022-03-01

AssignmentAndDefaultValueOfArray类

package day0301;
/**
 * JVM:栈内存(笼屉),堆内存(仓库)
 * 栈(变量名)相当于遥控器[存放地址],摇动到堆内存(数值)中
 * 
 * 数组理解:把多个相同类型的变量放到一块进行管理,形成一个数组
 * 基本数据类型声明的变量就像是杯子,可以存放这种指定类型的液体
 * 数组就像是一个杯架,上面放上指定数量的这种类型的杯子
 * 语法:
 * 		1.声明:数据类型[] 数组名称(变量名称); 举例:int[] array; 指的是声明一个存储int型数据的数组
 * 		2.赋值:数组名 = new 数据类型[length]; 举例:array = new int[3];
 * 		3.连写:数据类型[] 数组名 = new 数据类型[length]; 举例:byte[] bytes = new byte[3];
 * 特性:
 * 		1.数组存储的数据,类型一致
 * 		2.数组的长度在赋值的时候就固定了
 * 		3.数组是引用数据类型(用new,在堆内存里面开辟空间),引用数据类型是在栈内存存储一个内存地址,这个地址指向堆内存存放实际数据的位置
 * 数组名.长度,故下标最大为array[length - 1]
 * @author Katrina
 */
public class AssignmentAndDefaultValueOfArray {

	public static void main(String[] args) {	
		/*第一种声明赋值的方式*/
		/*
		 * 第一种循环遍历,利用array.length进行判定
		 * 格式:for (初始变量; 比较条件; 增量表达式) {执行语句}
		 * 比较的是变量与数组长度
		 */
//		int[] array = new int[5]; //声明一个int型数组,初始长度为5
//		for (int i = 0; i < array.length; i++) { //循环便利数组输出每一位的值
//			System.out.println("数组第" + (i + 1) + "个元素是" + array[i]);
//		}
		/*
		数组第1个元素是1
		数组第2个元素是2
		数组第3个元素是3
		数组第4个元素是4
		数组第5个元素是5
		 */
		/*对数组的单个元素进行赋值*/
//		array[0] = 1; 
//		array[1] = 2;
//		array[2] = 3;
//		array[3] = 4;
//		array[4] = 5;
//		for (int i = 0; i < array.length; i++) {
//			System.out.println("数组第" + (i + 1) + "个元素是" + array[i]);
//		}
		
		/*第二种声明赋值的方式*/
		/*
		 * 数据类型[] 数据名 = {符合数据类型的一个个值};
		 * 每个值之间用“,”隔开,值的个数是数组的长度
		 */
//		int[] nums = {3, 4, 5, 6, 7};
//		for (int n : nums) {
//			System.out.print(n + " "); //3 4 5 6 7 
//		}
		
		/*第三种声明赋值的方式*/
		/*
		 * 数据类型[] 数组名 = new 数据类型[] {符合数据类型的每一个值}
		 * 每个值之间用“,”隔开,值的个数是数组的长度
		 */
//		char[] charArray = new char[] {'A', 'B', 'C', 'D', 'E'};
//		for (char c : charArray) {
//			System.out.print(c + " "); //A B C D E 
//		}
		
//		int nums[] = new int[3]; //一般不推荐使用
		
		/*数据类型的默认值*/
		int[] numArray = {};
		System.out.println(numArray.length); //0
		int[] numArray2 = new int[0];
		System.out.println(numArray2.length); //0
	}
	
}

ArrayReferenceDemo类

package day0301;

public class ArrayReferenceDemo {

	public static void main(String[] args) {
		/*数组的引用*/
		/*
		 * 一个数组引用(赋值操作)另一个数组时,两者共享同一块堆内存
		 * 也就是共享数据,任意一个数组的某一位元素的变化,另一个数组相应的位置的元素也会变化
		 */
		int[] intArray1 = {5, 6, 7};
		int[] intArray2 = intArray1;
		for (int a : intArray2) {
			System.out.print(a + " "); //5 6 7
		}
		System.out.println();
		intArray2[1] = 8;
		for (int a : intArray2) {
			System.out.print(a + " "); //5 8 7
		}
		System.out.println();
		for (int a : intArray1) {
			System.out.print(a + " "); //5 8 7,引用数据类型,intArray1也跟着改变
		}
		
		System.out.println();
		
		/*基本数据类型的赋值*/
		/*
		 * 把赋值给另一个变量之后,另一个变量的修改,不影响原变量的值 
		 */
		int a = 1;
		int b = a;
		System.out.println("b的初始值:" + b); //b的初始值:1
		b = 2;
		System.out.println("b修改后的值:" + b); //b修改后的值:2
		System.out.println("b修改后a的值:" + a); //b修改后a的值:1
	}
	
}

ArrayCopyDemo类

package day0301;

public class ArrayCopyDemo {

	public static void main(String[] args) {
		/*数组的拷贝*/
		/*
		 *  System.arraycopy(src, srcPos, dest, destPos, length);
		 *  System.arraycopy(原始数组, 拷贝的原始数组起始位置, 拷贝的对象, 拷贝对象的起始位置, 拷贝的长度);
		 */
		//在堆当中生成了两块内容
		int[] arr1 = {1, 2, 3, 4, 5, 6};
//		int[] arr2 = {1, 2, 3, 4, 5, 6};
//		int[] arr2 = null; //报错
//		int[] arr2 = {}; //报错
		int[] arr2 = new int[7];
//		System.arraycopy(arr1, 0, arr2, 0, 2);
		//从数组1种的0开始的位置,复制2个元素到数组2种,
		//从0号下标(第一个位置)开始填充
//		for (int i : arr2) {
//			System.out.print(i + " ");
//		}
		//拷贝5个数
//		System.arraycopy(arr1, 0, arr2, 0, 5); //如果arr2的长度为4【arr->int[4]】,报错,ArrayIndexOutOfBoundsException,数组越界
		System.arraycopy(arr1, 0, arr2, 0, 4);
		for (int i : arr2) {
			System.out.print(i + " "); //1 2 3 4 0 0 0 
		}
		
		System.out.println();
		
		//赋值引用
		int[] arr3 = arr2;
		for (int i : arr3) {
			System.out.print(i + " ");
		}
		System.out.println();
		arr3[4] = 8;
		for (int i : arr2) {
			System.out.print(i + " ");
		}
	}
	
}

ForEachDemo类

package day0301;

public class ForEachDemo {

	public static void main(String[] args) {
		/*第二种遍历数组的方式*/
		/*
		 * for (数据类型 自定义变量名  : 要遍历的数组名称) {自定义变量就是数组的一个个元素}
		 * 注意:
		 * 		1.顺序输出,从数组的第一个元素开始
		 * 		2.利用数组名,不比较个数,全部遍历
		 */
//		int[] array = new int[5];
//		array[0] = 1; 
//		array[1] = 2;
//		array[2] = 3;
//		array[3] = 4;
//		array[4] = 5;
//		for (int num : array) {
//			System.out.print(num + " "); //1 2 3 4 5 
//		}
		
		/*例:*/
		//定一个int型数组,存放1 3 5 7 9,要求用第二种遍历输出数组的每一个值
		//用第一种循环遍历方式倒序输出
		int[] array1 = {1, 3, 5, 7, 9};
		for (int i : array1) {
			System.out.print(i + " ");
		}
		System.out.println();
		for (int i = array1.length - 1; i >= 0; i--) {
			System.out.print(array1[i] + " ");
		}
	}
	
}

Homework类

package day0301;

public class Homework {

	public static void main(String[] args) {
		/*
		 * 1.给定一个int型数组array,存放数据12, 23, 34, 45, 55, 96,输出数组里面的奇数
		 */
//		int[] array = {12, 23, 34, 45, 55, 96};
//		for (int i = 0; i < array.length; i++) {
//			if (array[i] % 2 == 1) {
//				System.out.print(array[i] + " "); //23 45 55 
//			}
//		}
		
		/*
		 * 2.给定一个int型数组array,存放数据9, 8, 3, 5, 10, 12,倒序输出里面的数据
		 */
		int[] array = {9, 8, 3, 5, 10, 12};
		for (int i = array.length - 1; i >= 0; i--) {
			System.out.print(array[i] + " "); //12 10 5 3 8 9 
		}
	}
	
}

2022-03-02

StringDemo类

package day0302;

import java.util.Arrays;

/**
 * String字符串
 * String的比较:引用数据类型,==比较的存储的内存地址
 * 重点:拼接,比较,转换成字符数组tochararray,取字串substring
 * @author Katrina
 */
public class StringDemo {

	public static void main(String[] args) {
//		String str1 = "Hello World"; //新建String
//		String str2 = new String("Hello World"); //用new的方式新建String
//		System.out.println(str1 == str2); //false
//		String str3 = "Hello World";
//		System.out.println(str1 == str3); //true
//		System.out.println(str2 == str3); //false
		
		/*常量通过两种方式赋值为"test"*/
//		String str4 = "test";
//		String str5 = new String("test");
//		System.out.println(str4 == str5); //false
		
		/*1.拼接 + */
		String s = "Hello";
		int a = 1;
		System.out.println(s + a); //Hello1,string与int拼接
		String t = "world";
		System.out.println(s + t); //HelloWorld,string与string拼接
		
		/*2.@@比较*/
		/*
		 * 判断是否相等有两种==(比较地址)和equals(比较实际值)
		 */
		String ss = "Helloworld";
		System.out.println(ss == (s + t)); 
		//false,s+t通过变量形式的拼接,编译时不确定它的值,所以是新建的对象
		System.out.println(ss == ("Hello" + "world")); 
		//true,"Hello" + "world"通过字符串常量的拼接,编译时可以确定值,不会新建
		System.out.println(ss.equals(s + t)); 
		//true,equals比较字面量,也就是看到的值
		System.out.println(ss.equals("Hello" + "world")); 
		//true
		
		/*3.返回字符串池的实例*/
		"hello".intern(); 
		
		/*4.字符操作*/
		//字符操作1:转成字符型数组 字符串.toCharArray
		char[] c = new char[3];
		char[] ch = s.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			System.out.print(ch[i] + " ");
		}
		
		System.out.println();
		
		//字符操作2:取某个位置的字符 charAt 位置:0~length-1
		char cha = s.charAt(0); //改为5,数组越界
		System.out.println(cha); //Hs
		System.out.println("Test".charAt(0)); //T
		
		/*5.取子串 */
		/*
		 * 字符串.subString(int start) 从start开始到字符串结尾取为字串
		 * start是索引,从0开始
		 */
		System.out.println(s.substring(0)); //Hello
		System.out.println(s.substring(2)); //第三位开始,llo
		/*
		 * 字符串.subString(int start, int end) 从start开始到字符串结尾取为字串
		 * start是索引,从0开始,end是结尾,并且不包含end 【前面>=,后面<】
		 * Hello 01234 其中23下标的位置为ll
		 */
		System.out.println(s.substring(2, 4)); //ll
		
		/*查找某个字串的(起始)位置 */
		/*
		 * 字符串.indexOf()
		 * 在字符串中寻找子串,找到返回首字符所在的位置,找不到返回-1
		 */
		System.out.println("Hello".indexOf("lo")); //3
		System.out.println("Hello".indexOf("llo")); //2
		System.out.println("Hello".indexOf("st")); //-1
		
		/*大小写转换*/
		/*
		 * 转大写toUpperCase() 转小写toLowerCase()
		 */
		System.out.println("hEllo".toUpperCase()); //HELLO
		System.out.println("HellO".toLowerCase()); //hello
		
		/*替换*/
		/*
		 * 字符串.replace(旧字符,新字符)
		 */
		System.out.println("hello".replace('l', 'r')); //herro
		
		/*分隔*/
		/*
		 * 字符串.split("分割的标准") 返回的类型是字符串数组
		 * 如下:按照d进行分割 split
		 */
		String[] strs = "asdasdssxs".split("d");
		for (String str: strs) {
			System.out.print(str + " "); //as as ssxs 
		}
		
		System.out.println();
		
		String[] strs1 = "asdasdssxs".split(""); //特殊情况,每一位分割
		for (String str: strs1) {
			System.out.print(str + " "); //as as ssxs 
		}
		
		System.out.println();
		
		String[] ssStrings = "Hello".split("");
		System.out.println(Arrays.toString(ssStrings)); //a s d a s d s s x s 
		
		String sss = "hello";
		System.out.println(sss.startsWith("h")); //true
		System.out.println(sss.startsWith("e", 1)); //true
	}
	
}

StringBuilderAndBufferDemo类

package day0302;
/**
 * StringBuilder与StringBuffer字符串拼接
 * 同:字符串拼接,使用这种拼接方式不需要创建新的对象,节省内存,提高效率
 * 异:StringBuilder线程不安全;StringBuffer线程安全
 * 注意:(1)如果要操作少量的数据用 String
 * (2)多线程操作字符串缓冲区下操作大量数据 StringBuffer
 * (3)单线程操作字符串缓冲区下操作大量数据 StringBuilder
 * @author Katrina
 */
public class StringBuilderAndBufferDemo {

	public static void main(String[] args) {
//		String str = "hello";
//		str = str + "world";
//		System.out.println(str);
		
//		StringBuilder sb = new StringBuilder(); //新建初始为空的StringBuilder
//		sb.append(str); //拼接
//		System.out.println(sb); //helloworld
		
		//新建StringBuilder对象,初始值 = String类型的str变量的值
//		StringBuilder sb2 = new StringBuilder(str);
//		sb2.append("123");
//		System.out.println(sb2); //helloworld123
		
//		StringBuffer stringBuffer = new StringBuffer(str); //新建StringBuffer
//		stringBuffer.append(str);
//		System.out.println(stringBuffer); //helloworldhelloworld
		
		//练习:定义两个String对象,赋值为"hello"和"world",用以上两种方式进行拼接,输出拼接后的结果
		String str1 = "hello";
		String str2 = "world";
		StringBuilder stringBuilder = new StringBuilder(str1);
		stringBuilder.append(str2);
		System.out.println(stringBuilder); //helloworld
		StringBuffer stringBuffer = new StringBuffer(str1);
		stringBuffer.append(str2);
		System.out.println(stringBuffer); //helloworld
	}
	
}

ConversionOfStringsToOtherTypes类

package day0302;

public class ConversionOfStringsToOtherTypes {

	public static void main(String[] args) {
		/*String与double的转换*/
		/*
		 * double类型转化成String,用String.valueOf(其他类型的变量)
		 */
		double d = 3.7;
		String s = String.valueOf(d);
		/*int型转换成String*/
		int i = 10;
		String s1 = String.valueOf(i); 
		/*boolean型转换成String*/
		boolean b = true;
		String bs = String.valueOf(b); 
		/*StringBuffer类型转换成String*/
		StringBuffer buffer = new StringBuffer("hello");
		String sbuffer = String.valueOf(buffer); 
		
		/*String转double的转换*/
		/*
		 * 利用double的封装类 Double.parseDouble(String)
		 */
		String strr = "3.6";
		double ds = Double.parseDouble(strr); 
		System.out.println(ds);
	}
	
}

Exercise类

package day0302;

public class Exercise {

	public static void main(String[] args) {
		/*字符串拼接*/
//		String a = "test";
//		String b = "3";
//		String c = a + b; 
//		System.out.println(c); //test3
//		String d = "test" + "3";
//		System.out.println(d); //test3
//		String e = a + 2 + 1;
//		System.out.println(e); //test21
//		String f = a + (2 + 1);
//		System.out.println(f); //test3
//		String g = a.concat("3");
//		System.out.println(g); //test3
		
		/*课堂练习*/
		String a = "test";
		String b = "test";
		String c = new String("test");
		String d = "te" + "st";
		String e = "te";
		String f = "st";
		String g = e + f;
		System.out.println(a == b); //true
		System.out.println(a.equals(b)); //true
		System.out.println(c == a); //false
		System.out.println(a.equals(c)); //true
		System.out.println(d == a); //true
		System.out.println(a.equals(d)); //true
		System.out.println(g == a); //false,同一个池子不同的变量
		System.out.println(a.equals(g)); //true
	}
	
}

Homework类

package day0302;

import java.util.Scanner;

public class Homework {

	public static void main(String[] args) {
		String str = "Java 技术学习班 20150628";
		System.out.println(str.substring(11, 19)); //20150628
		
		String str1 = "Hello Java";
		System.out.println(str1.replace("Java", "JavaEE")); //Hello JavaEE
		
		System.out.println(str.charAt(8)); //习
		
		System.out.println(str.replace("0", "")); //Java 技术学习班 215628

		Scanner sc = new Scanner(System.in);
		String str2 = sc.next();
		//37068220000704694X
		System.out.println(str2.substring(6, 14));
		
		String str3 = "want you to know one thing";
		char[] ch = str3.toCharArray();
		int n = 0;
		int o = 0;
		for (int i = 0; i < ch.length-1; i++) {
			if (ch[i] == 'n') {
				n++;
			} else if (ch[i] == 'o') {
				o++;
			}
		}
		System.out.println(n + " " + o); //4 4
		
		String username = sc.next();
		String password = sc.next();
		if (username.equals("admin") && password.equals("123456")) {
			System.out.println("登录成功");
		} else {
			System.out.println("登录失败");
		}
	}

}

2022-03-03

MethodDemo类

package day0303;
/**
 * 方法案例类
 * ①格式:
 * 		修饰符 返回值类型 方法名(参数类型 参数名,...){
 * 			//方法体:这个方法要干什么,执行什么操作
 * 			return 返回值;
 *		}
 * ②方法的三要素:返回值类型 参数列表 方法名称
 * ③方法使用的三种方式:直接调用、打印调用、赋值调用 
 * ④返回值说明:
 * 		1.返回值类型分两种:一种有返回,return的值一定要匹配这种返回类型;
 * 		一种无返回,定义的返回值类型填void,return语句可以写return;,也可以省略return语句
 * 		2.return放在方法的结尾,标志一个方法的结束,return语句后面加其他语句,程序会报错
 * 		3.参数变量分形参和实参,形参是定义方法时在参数列表中定义的,
 * 		形参的作用域是整个方法,调用方法时,传递得参数是实参,相当于把实参赋值给形参
 * @author Katrina
 */
public class MethodDemo {

	public static void main(String[] args) {
		int a = 1;
		int b = 2;
//		int num1 = (a + b) * (a + b);
		int num1 = method(a, b);
		System.out.println(num1); //9
		
		int c = 3;
		int d = 4;
//		int num2 = (c + d) * (c + d);
		int num2 = method(c, d);
		System.out.println(num2); //49
		
		/*三种调用格式*/
		out(); //hello 直接调用
		System.out.println(outNum(11)); //12 打印调用
		int e = outNum(10);
		System.out.println(e); //11 赋值调用
		
		//局部变量 f的作用域
		int f;
		f = 10;
//		int f = 9; //在局部变量的作用域内不能重复声明
		System.out.println(e + f);
	}
	
	public static int method(int a, int b) {
		int num = (a + b) * (a + b);
		return num;
	}
	
	public static void out() {
		System.out.println("hello");
//		return "hello"; //声明了void不能有返回
//		b++; //超出了局部变量b的作用域
//		return; //不报错
	}
	
	public static int outNum(int num) {
		return ++num; //num++ 返回仍然是10
//		return "hello"; //报错,返回值类型不是String
//		num++; //报错,return语句后面不能写其他语句
	}
	
}

ValuePassingAndReferencePassing类

package day0303;
import java.util.Arrays;
/**
 * 传值和传引用类
 * 基本数据类型的变量作为实参,传递给方法内的形参时,传递的是值
 * 方法内的形参的值的变化不影响原来实参的值
 * @author Katrina
 */
public class ValuePassingAndReferencePassing {
	
	public static void main(String[] args) {
		int a = 5;
		int b = increase(a); //a实参,num形参
		System.out.println(b); //6
		System.out.println(a); //5
		
		//引用数据类型的变量 作为实参,传递给方法内的形参时,传递的是引用地址
		//方法内形参的值发生变化,地址不变,所以对应实参指向的同样地址的值
		int[] arr = {1, 2, 3};
		int c = increase(arr); //方法调用时传递给方法的是实参
		System.out.println("c = " + c); //c = 2
		System.out.println("数组的元素 = " + Arrays.toString(arr)); //数组的元素 = [2, 2, 3]
	}
	
	/**
	 * 增长
	 * @return
	 */
	public static int increase(int num) { //方法声明时参数列表里面是形参
		return ++num;
	}
	
	public static int increase(int[] array) {
		return ++array[0];
	}
	
}

LocalVariableScope类

package day0303;
/**
 * 局部作用域类
 * @author Katrina
 */
public class LocalVariableScope {
	
	public static void main(String[] args) {
		System.out.println(getSalary(23)); //720 实参 23
		//一周上了7天得了多少分
		System.out.println(score(7)); //370
	}
	
	public static int getSalary(int days) { //形式参数 days
		int rate = 30; //每天的基本钱数
		int result = days * rate;
		if (days > 22) {
			int bonus = (days - 22) * rate;
			result = result + bonus;
		}
//		bonus++; //超出作用域
		return result;
	}
	
	/**
	 * 5天课,一天50分,超过一天课额外给10分
	 */
	public static int score(int days) {
		int score = 50;
		int totalScore = days * score;
		if (days > 5) {
			int ex = (days - 5) * 10;
			totalScore = totalScore + ex; 
		}
		return totalScore;
	}
	
}

EatDemo类

package day0303;

public class EatDemo {

	public static void main(String[] args) {
		System.out.println("小明今天" + eat("米粉"));
		System.out.println("小红今天" + eat("牛肉面"));
	}
	
	public static String eat(String food) {
		return "吃了" + food;
	}
	
}

Homework1类

package day0303;

public class Homework1 {

	public static void main(String[] args) {
		//能否写出两个方法名一致的方法? 如果可以请写出三个 add 方法分别计算两个整数相加,三个整数相加,两个浮点数相加,并测试
		int a = 1;
		int b = 2;
		int c = 3;
		double d = 1.1;
		double e = 1.2;
		System.out.println(add1(a, b));
		System.out.println(add1(a, b, c));
		System.out.println(add2(d, e));
	}

	private static int add1(int a, int b) {
		return a + b;
	}
	
	private static int add1(int a, int b, int c) {
		return a + b + c;
	}
	
	private static double add2(double a, double b) {
		return a + b;
	}
	
}

Homework2类

package day0303;

import java.util.Arrays;
import java.util.Scanner;

public class Homework2 {

	public static void main(String[] args) {
//		int[] oldArray = {-5, 19, 23, 37, 47, 56};
//		//从oldArray数组中查找35,如果35不存在,则返回插入点
//		int i = Arrays.binarySearch(oldArray, 35);
//		//根据插入点计算插入位置
//		int insertionIndex = -(i) - 1;
//		int[] newArray = new int[oldArray.length + 1];
//		//将插入位置之间的元素拷贝到新数组
//		for (int index = 0; index < insertionIndex; index++) { //3
//			newArray[index] = oldArray[index];
//		}
//		//将35添加到新数组中
//		newArray[insertionIndex] = 35;
//		//将插入位置之后的元素拷贝到新数组
//		for (int index = insertionIndex + 1; index < newArray.length; index++) {
//			newArray[index] = oldArray[index - 1];
//		}
//		System.out.println(Arrays.toString(oldArray));
//		System.out.println(Arrays.toString(newArray));
		
		Scanner scanner = new Scanner(System.in);
		char[] zods = {'猴', '鸡', '狗', '猪', '鼠', '牛', '虎',
				'兔', '龙', '蛇', '马', '羊'};
		while (true) {
			System.out.println("请输入您的出生年份,0退出");
			int year = scanner.nextInt();
			if (year == 0) {
				break;
			}
			int index = year % 12;
			char zod = zods[index];
			System.out.println("您的出生年份是:" + year + ",属" + zod);
		}
	}
	
}

Homework3类

package day0303;
import java.util.Arrays;
public class Homework3 {

	public static void main(String[] args) {
		int a = 0;
		int[] arr1 = {4, 2, 5, 8, 3, 10, 1};
		for (int i1 = 0; i1 < arr1.length - 1; i1++) {
			for (int j = 0; j < arr1.length - 1 - i1; j++) {
				if (arr1[j] > arr1[j + 1]) {
					a = arr1[j];
					arr1[j] = arr1[j + 1];
					arr1[j + 1] = a;
				}
		    }
		}
		for (int i2 = 0; i2 < arr1.length; i2++) {
			System.out.print(arr1[i2] + " ");
		}
		System.out.println();
		Arrays.sort(arr1); //sort排序,默认升序
		for (int arr : arr1) {
			System.out.print(arr + " ");
		}
		System.out.println();
		int[] array = {4, 2, 5, 8, 3, 10, 1};
		System.out.println("原数组:");
		for (int i : array) {
			System.out.print(i + " ");
		}
		System.out.println();
		selectSort(array);
		System.out.println("排序后:");
		for (int i : array) {
			System.out.print(i + " ");
		}
	}
		
	public static void selectSort(int[] arr) {
		for (int i = 0; i < arr.length - 1; i++){
			int min = i;
			for (int j = i + 1; j <arr.length; j++){
				if (arr[j] < arr[min]) {
					min = j;
				}
			}
			if (min != i) {
				swap(arr, i, min);
			}
		}
	}
	
	//完成数组两元素间交换
	public static void swap(int[] arr,int a,int b){
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}
	
}

Homework4类

package day0303;

import java.util.Arrays;
import java.util.Scanner;

public class Homework4 {

	public static void main(String[] args) {
		  Scanner in = new Scanner(System.in);
		  int [] a = {1, 3, 4, 6, 9, 18, 22};
		  System.out.println("请输入一个数:");
		  int b = in.nextInt();
		  int i = Arrays.binarySearch(a,b);
		  if (i >= 0) {
			  System.out.println("该数在数组的下标为:" + i);
		  } else {
			  System.out.println("-1");
		  }
	}
	
}

Homework5类

package day0303;

import java.util.Arrays;

public class Homework5 {

	public static void main(String[] args) {
        int[] arr = new int[]{34, 47, 10, 2, 33, 11, 66, 78};
        int[] result = count(arr);
        int[] even = new int[result[0]];
        int[] odd = new int[result[1]];
        int evenId = 0;
        int oddId = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                even[evenId] = arr[i];
                evenId++;
            } else {
                odd[oddId] = arr[i];
                oddId++;
            }

        }
        Arrays.sort(even);
        Arrays.sort(odd);
        System.out.println(Arrays.toString(even));
        System.out.println(Arrays.toString(odd));
    }
	
	public static int[] count(int[] arr) {
        int[] result = new int[2];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) { //偶数
                result[0]++;
            } else {
                result[1]++;
            }
        }
        return result;
    }
	
}

Homework6类

package day0303;

public class Homework6 {

	public static void main(String[] args) {
		double[] arr = new double[]{8.0, 9.9, 9.0, 8.9, 9.2};
        double max = arr[0];
        double min = arr[0];
        double sum = 0.0;
        for (int i = 0; i <arr.length-1; i++) {
            if (max < arr[i]) {
                max = arr[i];
            } if (min > arr[i]){
                min = arr[i];
            }
            sum = sum + arr[i];
        }
        double average = (sum - max - min) / 8.0;
        System.out.printf("平均分为:%.2f", average);
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值