Java学习笔记
Java都是值传递(面试题)
1、IF条件语句
有if又有else if时,else必须放在最后面,当其中任意一个else if满足为true时,那么其他所有的else if和else都不运行
public class scannerStr { //对于Scanner的next和nextLine的区别 //next遇到空白字符则结束读取字符,而nextLine则遇到回车符才会结束读取 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请键入成绩:"); int score = scanner.nextInt(); if(score<=100&&score>=90) System.out.println("A"); else if(score<90&&score>=80) System.out.println("B"); else if(score<80&&score>=70) System.out.println("C"); else if(score<70&&score>=60) System.out.println("D"); else if(score<60&&score>=0) System.out.println("不合格"); else System.out.println("不合法成绩"); //关闭io流 scanner.close(); } }
2、while语句
public class Test { public static void main(String[] args) { //累加1~100 int i=0; int sum=0; while(i<=100) { sum = sum + i; i++; } System.out.println(sum); } }
3、for循环计算0到100的奇数和、偶数和
public class Test { public static void main(String[] args) { //输出0~100的奇数和、偶数和 int i1=0; int sum1=0; int sum2=0; for (i1 = 0; i1 <= 100; i1++) { if(i1%2!=0) { sum1 += i1; } else { sum2 += i1; } } System.out.println("奇数和:"+sum1); System.out.println("偶数和:"+sum2); } }
3.1 强For循环
//强For循环就是把后面的数组内容直接赋值给一个变量,最后将其输出即可,是一个偷懒型的用法 public class Test { public static void main(String[] args) { int[] numbers = {10,20,30,40,50}; for (int i = 0; i < 5; i++) { System.out.println(numbers[i]); } System.out.println("==================="); for (int x: numbers){ System.out.println(x); } } }
4、九九乘法表
//思考前版 public class Test { public static void main(String[] args) { int line = 1; //控制行 for (int i = 1; i <= 9; i++) { //控制列,并且去除了重复项,如果j的条件依旧是小于10,那么很多重复项,例如图1,去除后,例如图2 for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + (i * j) + "\t"); } //控制换行,第一行换,line自增,第二行全输出完后,换行,line继续自增,i和line是同步满足取模运算 if (line % i == 0) System.out.println(); line++; } } } //更新版: public class Test { public static void main(String[] args) { int line = 1; //控制行 for (int i = 1; i <= 9; i++) { //控制列 for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + (i * j) + "\t"); } //因为line和i是同步自增,那根本不需要去取模,因为一定取模为0,所以直接在每次的行输出完毕后,跳出内循环后,直接换行即可。 System.out.println(); } } }
图1
图2
4.1 思考
-
计算机先是打印第一列,也就是让变量先设置成常量
-
先打印第一行
-
未去除重复项的效果
-
-
如何去除重复项 —— j<=i ,让固定变成动态变化量,如何内外嵌套
5、Break、Continue语句
break直接强制跳出循环,不执行剩余语句,而continue则是退出本次循环,不执行本次循环的循环内容,进行下一次循环。
6、打印101-500之间所有的质数
质数:在大于1的自然数中,除了1和它本身之外没有其他任何因素的自然数
//虽然java没有Goto关键字语句,但是可以有“标签式”语句,同可用于跳转 //大于一个数的一半了,那么就一定没有多的因数可以和它一起组成,所以内层循环只需要到其一半 public class Test { public static void main(String[] args) { //打印101-500之间所有的质数 //质数是在大于1的自然数中,除了1和他本身之外没有其他任何因数的自然数 int num = 0; outer: for (int i = 101; i < 501; i++) { for (int j = 2; j < i / 2; j++) { if (i % j == 0) { continue outer; } } System.out.print(i + "\t"); num++; if (num % 5 == 0) System.out.println(); } } }
7、输出三角形
输出左半边后(前两个for循环),再输出右半边(后一个for循环)
public class Test { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { //依次输出5个、4个.....1个空格 for (int j = 5; j >= i; j--) { System.out.print(" "); } //依次补充1个、2个....5个* for (int j = 1; j <= i; j++) { System.out.print("*"); } //依次补充0个、1个....4个* for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); } } }
输出结果:
8、方法的调用
-
首先静态方法的main函数,若想调用其他自定义函数,那么应该让此自定义函数也为static静态方法,或者使用new对象进行调用
//若add方法,不加static可能导致编译失败 public class method { public static void main(String[] args) { //实际参数,实际调用方法传递给他的参数 int sum = add(1,2); System.out.println(sum); } //形式参数,只用于定义作用 public static int add(int a,int b) { return a + b; } }
9、方法的重载
-
一个类里有两个方法,只需要确保参数列表不同(参数类型、参数个数不同、参数排列顺序不同),但名字可以相同;但方法名称相同才叫做重载!!
-
//命令行 cd 是进入 /* java的原始调用,使用命令行!javac命令是将源代码编程成class字节码文件!java源文件有包结构的时候,再使用javac命令编译,就需要一些参数!例如{} 一定要找到包的位置 1.首先现在最内层的文件目录处进行编译文件 javac Test.java 2.退后到包名的最外层,如src层 3.在此可以进行java文件的运行操作 如:java com.mao.day02.Test {这后面为输入的内容,供程序使用}{this is mao} 4.如果程序只是去遍历此数组,那么则依次输出this 、 is 、 mao */
10、数组定义
/* 通过new操作符来创建数组 int[ ] Array == int Array[ ] ====== 但是推荐第一种 数据类型[ ] 数组变量名 = new 数组名[数组大小] int[ ] array = new Array[10] */ public class Array { public static void main(String[] args) { //静态初始化数组,创建 + 赋值 int[] a = {1, 2, 3, 4, 5, 6}; for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "\t"); } System.out.println(); //动态初始化数组,未初始化的则系统默认赋值,也就是0 int[] array = new int[10]; array[0] = 10; for (int i = 0; i < array.length; i++) { System.out.print(array[i] + "\t"); } } }
10.1 数组的打印、数组的反转(利用方法体进行调用)
public class ArrayDemo { public static void main(String[] args) { int[] array = new int[5]; array[0] = 1; array[1] = 2; array[2] = 3; array[3] = 4; array[4] = 5; //主函数直接调用“打印数组”方法 printArray(array); //在主函数中创建反转后的数组用于输出 int[] reverse = reverse(array); //再把反转方法函数中的结果赋值给reverse数组,参数的传递 printArray(reverse); } //打印数组 public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + "\t"); } System.out.println(); } //反转数组 public static int[] reverse(int[] array) { //创建一个动态数组(用于去存储要反转数组中的元素) int[] result = new int[array.length]; System.out.println("array数组的长度为:" + array.length); System.out.println("反转后的array数组为:"); //从旧数组中的最后一个元素依次赋值给新数组的第一个元素(以此类推) for (int i = 0, j = result.length - 1; i < array.length; i++, j--) { result[j] = array[i]; } //最后返回一个完整的数组,也就是调用这个方法最后得到的就是一个数组 return result; } }