资讯详情

第二周-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.p

标签: 0228连接器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台