Java学习笔记1

Java学习笔记

学习一门新的语言,就是从helloworld开始的,Java也是一样。

public class helloworld {
	
	public static void main(String[] args) {
		System.out.println("helloworld");
	}
}

学习新的语言就要了解其基本语法。以下代码是我学习过程中所练习所做的笔记。

public class helloworld {
	
	public static void main(String[] args) {
		System.out.println((char)('a' + 1));	
		System.out.println("hello" + 'a' + 1);	//字符串连接在一起
		System.out.println('a' + 1 + "hello");	//从左向右运算
		System.out.println("5 + 5 = " + (5 + 5));	//括号的优先级最高
		char c = '中';							//字符可以存储中文
		System.out.println(c);
		System.out.println(10 / 3);				
		System.out.println(10 % -3);
		int a = 1;
		System.out.println(++a);		// ++a先进行 + 1 再输出
		System.out.println(a++);		//先输出,再 + 1
	}
}

这是自增自减运算符所做的一些练习。

public class creasing {
	public static void main(String[] args) {
		int a = 10;
		int b = 10;
		int c = 10;
		a = b++;
		c = --a;
		b = ++a;
		a = c--;
		System.out.println("a = " + a + ",b = " + b + ",c = " + c);
		
		
		int x = 4;
		int y = (x++) + (++x) + (x * 10);
		System.out.println("y = " + y + ",x = " + x);

	}
}

有基本的输出语句,就会有就会有键盘输入。以下这是简单的键盘录入(整型)。

import java.util.Scanner;						//导入包中的类Scanner

public class scanner {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);	//创建键盘录入对象
		System.out.println("请输入一个整数:");
		int x = sc.nextInt();    				//将建盘录入的数据存储在x中
		System.out.println("x = " + x);
		int y;
		System.out.println("请再输入一个整数:");
		y = sc.nextInt();
		System.out.println("y = " + y); 
		/*
		int sum;
		sum = x + y;							//求和
		System.out.println("sum = " + sum);
		*/
		
		/*
		boolean a;
		a = (x == y);							//判断x、y是否相等 
		System.out.println(a);
		*/
		int z;
		System.out.println("请再输入一个数:");
		z = sc.nextInt();
		System.out.println("z = " + z);
		int max;
		max = (x > y) ? x : y;
		max = (max > z) ? max : z;
		System.out.println("max = " + max);
	}
}

接下来学习了运算符的应用(和C语言基本一样,但存在不同)。
这是关系运算符(结果是 true 或 false):

public class relation {
	public static void main(String[] args) {
		//关系运算符 得到的结果是true 或 false
		System.out.println(4 == 3);
		System.out.println(4 != 3);
		System.out.println(4 >= 3);
		System.out.println(4 <= 3);
		System.out.println(4 > 3);
		System.out.println(4 < 3);
	}
}

接下来是逻辑运算符(其结果也为 true 或 false):

public class logic {
	public static void main(String[] args) {
		// x > 5 & x < 15 表示 :5 < x < 15  
		//逻辑与  &  结果是 true 或 false   
		//遇 false 则 false 全 true 则 true
		 //&& 具有短路效果 与 & 结果一样   左边是 false 右边不执行
		int a = 10;
		int b = 20;
		int c = 30;
		System.out.println(a < b & b < c);
		System.out.println(a < b & b > c);
		System.out.println(a > b & b < c);
		System.out.println(a > b & b > c);
		System.out.println(' ');
		System.out.println(a < b && b < c);
		System.out.println(a < b && b > c);
		System.out.println(a > b && b < c);
		System.out.println(a > b && b > c);
		System.out.println(' ');
		//  | 逻辑或运算符
		// 遇 true 则 true 全 false 则 false
		//同理 || 与 | 结果一样
		//左边是 true 右边不执行
		System.out.println(a < b | b < c);
		System.out.println(a < b | b > c);
		System.out.println(a > b | b < c);
		System.out.println(a > b | b > c);
		System.out.println(' ');
		System.out.println(a < b || b < c);
		System.out.println(a < b || b > c);
		System.out.println(a > b || b < c);
		System.out.println(a > b || b > c);
		System.out.println(' ');
		// ^ 逻辑异或运算符
		//两边相同为 false 两边不同为 true
		System.out.println(a < b ^ b < c);		//false true - true
		System.out.println(a < b ^ b > c);		//true 	true - false
		System.out.println(a > b ^ b < c);		//true 	false - true
		System.out.println(a > b ^ b > c);		//false false - false
		System.out.println(' ');
		
		// ! 逻辑非
		//非真为假  非假为真
		System.out.println(!true);
		System.out.println(!false);
	}
}

还有位运算符:

public class bit_operation {
	public static void main(String[] args) { 
		 //位运算符  & 、|、^、~(按位取反) 
		 // & 有0 则为 0
		 // | 有1 则为 1
		 // ^ 相同则为 0,不同则为1
		 System.out.println(6 & 2);
		 /*
		 	1 1 0
		 	0 1 0
		 -----------
		 	0 1 0
		 */
		 System.out.println(6 | 2);
		 /*
		 	1 1 0
		 	0 1 0
		 -----------
		 	1 1 0
		 */
		 System.out.println(6 ^ 2);
		 System.out.println(6 ^ 2 ^ 2);
		 System.out.println(6 ^ 2 ^ 6);
		 /*
		  * 
		  * 位异或运算符
		  * 一个数据对另一个数异或两次,该数本身不变   (应用:可以将两数进行交换)
		  * x = x ^ y;
		  * y = x ^ y;
		  * x = x ^ y;
		 	1 1 0
		 	0 1 0
		 -----------
		 	1 0 0
		 */
		 System.out.println(~6);
		 /*
		  6的原码(反码、补码) 00000000 00000000 00000000 00000110
		  取反				   11111111 11111111 11111111 11111001	(结果的补码)
		  所以结果			   10000000 00000000 00000000 00000111 (-7)  
		 */
		 
		 /* << 左移 左边最高位丢弃,右边补齐0
		  * >> 右移 最高位是0,左边补齐0;最高位是1 左边补齐1
		  * >>> 无符号右移 无论最高位是 0 还是 1 ,左边补齐0
		  * 
		  * */
		 System.out.println(12 << 1);
		 System.out.println(12 << 2);
		 //向左移动几位就是乘以 2 的几次幂
		 // 	00000000 00000000 00000000 00001100   12
		 //  (0)00000000 00000000 00000000 00011000   24
		 
		 
		 System.out.println(12 >> 1);
		 System.out.println(12 >> 2);
		 //向右移动几位就是除以 2 的几次幂
		 //		00000000 00000000 00000000 00001100    12
		 //     00000000 00000000 00000000 00000110(0) 6
		 
		 
		 System.out.println(12 >>> 1);
	 }
}

还学习了选择结构语句。
选择结构包括 if 语句和 switch语句(和C语言基本上差不多)。
以下是 if 和 switch 的基本用法和一些小的练习。

import java.util.Scanner;

public class select {
	public static void main(String[] args) {
		/* 选择结构
		 * if语句
		 * switch语句
		 * 
		 *					if 语句 
		 * if(比较表达式) {
		 * 	语句体;
		 * }
		 * 如果比较表达式返回true 执行语句体
		 * 如果返回false 不执行语句体
		 * 比较表达式无论简单复杂,结果必须是boolean类型
		 * if 语句控制的语句体如果是一条语句,大括号可以省略(建议不要省略)
		 * 一般来说,有左大括号就没有分号,有分号就没左大括号
		 * 
		 *  int x = 10;		这是两句话
		 *  int x	声明语句
		 *  x = 10  赋值语句
		 * */
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		if(a > 18) {
			System.out.println(true);
		}
		/*
		 * if - else 语句
		 * if(比较表达式) {
		 * 	语句体1;
		 * } else {
		 * 	语句体2;
		 * }
		 * 若比较表达式为true
		 * 则执行语句体1
		 * 若表达式为false
		 * 则执行语句体2
		 * else 后不能加条件
		 * 
		 * if - else 语句可以代三元运算符
		 * 但三元运算符不能够代替if - else 语句
		 * */
		int b = sc.nextInt();
		if(b > 18 && b < 60) {
			System.out.println(true);
		} else {
			System.out.println(false);
		}
		/* if - else - if 语句
		 *  
		 * */
		int x = sc.nextInt();
		if(x > 0 && x < 18) {
			System.out.println("未成年");
		} else if(x < 60) {
			System.out.println("已成年,未变老");
		} else {
			System.out.println("已老");
		}
		//输入成绩,判断并输出成绩的等级
		int number;
		number = sc.nextInt();
		if(number <= 100 && number >= 85) {
			System.out.println("优秀");
		} else if(number >= 70) {
			System.out.println("良好");
		} else if(number >= 60) {
			System.out.println("及格");
		} else if(number >= 0) {
			System.out.println("不及格");
		} else {
			System.out.println("输入成绩不正确!");
		}
		
		/*				switch语句
		 * 
		 * switch(表达式) {		//基本数据类型可以接收byte 、short 、char 、int 不可以为long
		 * 	case 值1:			//引用数据类型可以接收枚举 String字符串
		 * 		语句体1;			//case 不能是变量,只能是常量
		 * 		break;			//break 不能省略
		 * 	case 值2:
		 * 		语句体2;
		 * 		break;
		 * 	case 值3:
		 * 		语句体3;
		 * 		break;
		 * 	case 值4:
		 * 		语句体4;
		 * 		break;
		 * 	……
		 * 	case 值n:
		 * 		语句体n;
		 * 		break;
		 * 	default :			//不论放在什么位置,都将最后一个执行  此语句中break可以省略
		 * 		语句体n+1;
		 * 		break;
		 * 
		 * 		 * 
		 *  
		 * }
		 * */
		String name = "张三";
		String gander = "男士";
		switch (gander) {
		case "男士":
			System.out.println(name + "是一位" + gander);
			break;
		case "女士":
			System.out.println(name + "是一位" + gander);
			break;	
		}
		/*if 语句 和switch 语句的各自使用场景
		 * switch语句建议判断固定值时使用
		 * if 语句建议判断区间或范围时使用
		 * */
		int month;
		month = sc.nextInt();
		switch (month) {
			case 3:
			case 4:
			case 5:
				System.out.println(month + "月是春季");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println(month + "月是夏季");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println(month + "月是秋季");
				break;
			case 1:
			case 2:
			case 12:
				System.out.println(month + "月是冬季");
				break;
			default :
				System.out.println("该月份不存在");
				break;
		}
		if(month > 12 || month < 1) {
			System.out.println("该月份不存在!");
		} else if(month > 2 && month < 6) {
			System.out.println(month + "月是春季");
		} else if(month > 5 && month < 9) {
			System.out.println(month + "月是夏季");
		} else if(month > 8 && month < 12) {
			System.out.println(month + "月是秋季");
		} else {
			System.out.println(month + "月是冬季");
		}
	}
}

学习了选择结构,接着学习了循环结构:
循环结构包括 for 语句,while语句 、do—while语句。

public class circulation {
	public static void main(String[] args) {
		/*			for循环
		 * for(初始化表达式;条件表达式;循环后的操作表达式){
		 * 		循环体;
		 * }
		 * 		1.先执行初始化语句
		 * 		2.再执行条件表达式
		 * 		 若返回true 执行循环体
		 * 		 若返回false 结束循环
		 * 		3.执行完循环体,在执行循环后的操作表达式
		 * 		4.从 2 步骤重新开始执行,直到结束循环	
		 * */
		int i ;
		for(i = 0 ;i < 10; i++) {		//打印十次helloworld
			System.out.println(i+1 + ".helloworld");
		}
		for(i = 1 ;i < 11; i++) {		//打印1 - 10
			System.out.println("i = " + i);
		}
		for(i = 10 ;i > 0; i--) {		//打印10 - 1
			System.out.println("i = " + i);
		}
		//	水仙花数
		// 水仙花数 指一个三位数,其各位数字的立方和等于该数本身
		int count = 0;						//计数器思想 :满足条件就自增
		for(i = 1;i < 10 ;i++) {
			for(int j = 0;j < 10 ;j++) {
				for(int z = 0;z < 10 ;z++) {
					if((i*i*i + j*j*j + z*z*z) == (i*100 + j*10 + z)) {
						count++;
						System.out.println(i*100 + j*10 + z);
					}
				}
			}
		}
		System.out.println(count);
		/*		while循环
		 * 	while(判断条件语句){
		 * 		循环体语句;
		 * 	}
		 * 	初始化语句;
		 * 	while(判断条件语句) {
		 * 		循环体语句;
		 * 		循环后的操作表达式
		 * 	}
		 * 	执行流程:
		 * 		1.先执行初始化语句
		 * 		2.再执行条件表达式
		 * 		 若返回true 执行循环体
		 * 		 若返回false 结束循环
		 * 		3.执行完循环体,在执行循环后的操作表达式
		 * 		4.从 2 步骤重新开始执行,直到结束循环	
		 * */
		
		int x = 1;
		int sum = 0;
		while(x < 101) {
			sum = sum + x;
			x++;
		}
		System.out.println("sum = " + sum);
		/*				do-while循环
		 * 初始化语句;
		 * do{
		 * 		循环体语句;
		 * 		控制条件语句;
		 * }while(判断条件语句);
		 * 执行流程:
		 * 		1.先执行初始化语句
		 * 		2.执行循环体语句
		 * 		3.执行控制条件语句
		 * 		4.执行判断条件语句
		 * 			若返回true 则继续执行循环体语句
		 * 			若返回false 则结束循环
		 * 		5.从 2 开始重新执行 
		 * 
		 * */
		i= 1;
		sum = 0;
		do {
			sum = sum + i;   
			i++;
		}while(i<101);
		System.out.println("sum = " + sum);
		/*	三种循环的区别
		 * do——while 与for 、while 的区别;
		 * 		do——while 至少执行一次
		 * 		for、while可能一次都不执行
		 * for循环可以在内部定义控制循环的变量,在循环结束时就将其释放掉
		 * */
		
		/*			死循环
		 * while(true) {
		 * 		循环体;
		 * }
		 * 
		 * for(;;) {
		 * 		循环体;
		 * }
		 * 	无限循环
		 * */
		
		
		/*			循环嵌套
		 * 
		 *		输出以下内容:		
		 *			*****
		 *			*****
		 *			*****
		 *			*****
		 *			*****
		 *
		 *System.out.println()  后面有一个换行符
		 *System.out.print()  	后面没有换行符
		 * */
		
		//正三角形
		for(int a = 0 ; a < 5; a++) {
			for(i = 5; i > a ; i--) {
				System.out.print(" ");
			}
			for(int b = 0 ; b < a+1 ; b++) {
				System.out.print("*");
			}
			for(int b = 1 ; b < a+1 ; b++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		/*		打印9 * 9乘法表
		 * */
		int j;
		for(i = 1;i <= 9;i++) {
			for(j = 1;j < i+1;j++) {
				System.out.print(j + " * " + i + " = " + (i * j) + "\t" );
			}
			System.out.println();
		}
		/*		break
		 * 只能在switch或循环中使用	
		 * 
		 * 		continue
		 * 只能在循环中使用
		 * 表示终止本次循环
		 * 
		 * 		return 
		 * 表示结束方法
		 * */
		
		/*		标号       ------合法标识符即可
		 * a: for(i = 1;i < 11; i++){
		 * 		System.out.println("i = " + i);
		 * 	 b: for(j = 1;j < 11; j++){
		 * 			System.out.pirntln("j = " + j);
		 * 			break a; 
		 * 		}
		 * 	  }
		 * 
		 * 
		 * 输出2次	你好
		 * 输出7次	你好
		 * 输出13次	你好
		 * 
		 * */
		for(i = 1;i < 11; i++) {
			if(i % 3 == 0) {
				//break;			//输出两次	
				//continue;		//输出7次
				System.out.println("你好");	//输出13次
			}
			System.out.println("你好");
		}
		
	}
		
}

接下来学习了方法(方法感觉就是C语言中的函数):

import java.util.Scanner;

public class idea {
	public static void main(String[] args) {
		int sum;
		int a = 1,b = 2;
		sum = add(a , b);
		System.out.println(sum);
		Scanner sc = new Scanner(System.in);
		a = sc.nextInt();
		b = sc.nextInt();
		print(a, b);
	}
	/*		方法
	 * 方法可以提高代码的通用性
	 * 方法是完成特定功能的代码块
	 * 方法的格式:
	 * 		修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2 ...) {
	 * 			方法体语句;
	 * 			return 返回值;
	 * 		}
	 * 修饰符:暂时使用	public static
	 * 返回值类型: 就是功能结果的数据类型
	 * 方法名: 符合规则即可
	 * 参数:实参、形参
	 * 方法相当于C语言中的函数
	 * 
	 * 		方法重载
	 * 重载是方法名相同,参数列表不同,与返回值类型无关
	 * 	参数个数不同
	 * 	参数类型不同(包括参数顺序不同)
	 * */
	public static int add(int a, int b) {
		int sum;
		sum = a + b;
		return sum;
	}
	public static int add(int a,int b,int c) {
		return a + b + c;
	}
	public static void print(int a, int b) {
		for(int i = 0;i < a;i++) {
			for(int j = 0;j < b;j++) {
				System.out.print('*');
			}
			System.out.print("\n");
		}
	}
}

方法之后,学习了数组,数组与C语言还是有所差异的:

import java.util.Scanner;

public class array {
	public static void main(String[] args) {
		//数组
		//数组格式:
		//	数据类型[] 数组名 = new 数据类型[数组长度];  new表示创建新的实体或对象
		int[] arr = new int[5];		// 动态初始化
		System.out.println(arr);	// [I@24273305  [ 表示数组	
									/*				I 表示int型
									 * 				@ 固定的
									 * 				24273305 表示地址 
									 * */
		int[] arr2 = new int[]{1,2,3};		//不能在后面的[]里定义长度
		int[] arr3 = {1,2,3};
		System.out.println(arr2);
		double[] ch = new double[2];
		System.out.println(ch);
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个数:");
		int a = sc.nextInt();
		
		//arr.length 表示数组长度
		for (int i = 0;i < arr.length;i++) {
			System.out.println(arr[i]);
		}
		int max;
		max = GetMax(arr3);
		System.out.println(max);
		arr3 = reverse(arr3);
		for(int i = 0;i < arr3.length;i++) {
			System.out.println(arr3[i]);
		}
	}
	
	public static int GetMax(int[] arr) {		//求数组中的最大值
		int max;
		max = arr[0];
		for(int i = 1;i < arr.length;i++) {
			if(max < arr[i]) {
				max = arr[i];
			}		
		}
		return max;
	}
	public static int[] reverse(int[] arr) {		//反转数组
		int i, j;
		i = 0;
		j = arr.length - 1;
		for (;i+1 < j;i++,j--) {
			int t;
			t = arr[i];
			arr[i] = arr[j];
			arr[j] = t;
		}
		
		return arr;
	}
}


还有二维数组:

/*		二维数组
 * 二维数组Java与C语言的区别
 *
 *Java中每行可表示不同长度
 *C语言中二维数组每行的长度是相等的
 */


public class double_array {
	public static void main(String[] args) {
		/*
		 * 二维数组的格式
		 * 		int [][] arr = new int[3][2];
		 * 		int [] arr [] = new int[3][2];
		 * 		int arr[][] = new int[3][2];
		 * */
		int[][] arr = new int[3][2];
		System.out.println(arr);
		System.out.println(arr[0]);
		System.out.println(arr[0][0]);
		/*	[[I@24273305	二维数组的地址值
		 *	[I@5b1d2887		第一个一维数组的地址值
		 *	0				第一个元素的值
		 * */
		int[][] arr1 = new int[3][];
		System.out.println(arr1[0]);
		arr1[0] = new int[3];
		arr1[1] = new int[2];
		arr1[2] = new int[1];
		System.out.println(arr1[0]);
		for(int i = 0;i < arr1.length ; i++) {
			for(int j = 0; j < arr1[i].length ; j++) {
				System.out.print(arr1[i][j]);
			}
			System.out.println();
		}
	}

}

次博客仅是自己的一点点笔记,供本人今后复习使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值