循环、方法和数组的整理

1.三目运算符

语法格式:数据类型 x = (表达式) ? value1 : value2;

执行流程: 先判断表达式如果表达式为true 就将value1赋值给x这个变量, 如果为false 就将value2赋值给变量x。

public class Demo1 {
	public static void main(String[] args) {
		int a = 30;
		int c;
		//int c = a > 31 ? 20 : 21;
		//执行流程:  判断a > 31 的值  如果 是true 将 20赋值给c     如果是false 将21 赋值给c
		//System.out.println(c);
		//三目运算符和if-else  类似
		
		if (a > 31) {
			c = 20;
		} else {
			c = 21;
		}
		System.out.println(c);
		
		
	}
}

2.循环结构

2.1为什么会有循环结构

        如果代码中出现了大量的重复的代码或者有规律的代码的时候,可以使用循环结构出来让代码变得简洁。

        不使用循环的缺点:

                ①代码看着红臃肿

                ②阅读性差

                ③可以维护低

        循环可以解决以上所有的问题

2.2while循环

        语法格式:while (布尔表达式) { 语句体 }

        执行流程: 当代码执行到while关键字的时候,先去判断布尔表达式,如果布尔表达式为false,就不会执行语句体。如果布尔表达式为true 执行语句体,然后(重要) 再去判断布尔表达式,如果布尔表达式为true执行语句体,然后再去判断布尔表达式,直到布尔表达式为false的时候,循环就结束了(就不再执行语句体了)。接着执行循环以外的代码。

public class Demo3 {
	public static void main(String[] args) {
		//打印4遍的中午吃烤羊排,使用while循环
		
		//无论任何循环  都要必须有三个条件:
			//1.初始化条件
			//2.循环条件
			//3.终止条件
		
		/**
		 * 分析代码,主要是分析执行的流程:
		 * i=0   0<4  true  执行大括号里面的代码  打印第一次的  中午吃烤羊排  i++
		 * i=1   1<4 true 执行大括号里面的代码  打印第二次的  中午吃烤羊排  i++
		 * i=2   2<4 true 执行大括号里面的代码  打印第三次的  中午吃烤羊排  i++
		 * i=3   3<4 true执行大括号里面的代码  打印第四次的  中午吃烤羊排  i++
		 * i=4  4<4 false  就不再执行大括号中的代码了!!!
		 */
		//1.声明一个变量
		int i = 0;//初始化条件
		while (i < 4) {// i <  4  终止条件   
			System.out.println("中午吃烤羊排");
			i++;//i++  是循环条件  自身加1
		}
		
		
	}
}

打印1~10的奇数 1 3 5 7 9...


public class Demo6 {
	public static void main(String[] args) {
		//打印10以内的奇数
		
		/**
		 * i = 1  1<11 true  执行if语句 1%2 !=0 true  执行if后面的大括号的代码 sout(1) i++
		 * i=2    2<11 true 执行if语句 2%2 !=0 false  if后面的大括号就不再执行了 i++
		 * i= 3   3<11 true  执行if语句 3%2 !=0 true 执行if后面的大括号的代码 sout(3) i++
		 * i=4  4<11   true执行if语句 3%2 !=0  false  if后面的大括号就不再执行了 i++
		 * i=5  
		 * 
		 * 循环了10遍
		 */
		int i = 1;
		while (i < 11) {
			//while循环可以嵌套if语句
			if (i % 2 != 0) {
				System.out.println(i);
			}
			i++;
		}
		
		System.out.println("============");
		
		/**
		 * a = 1  1<11 true   sout(1) a = a+2  a=3
		 * a= 3  3 <11 true sout(3) a=a+2  a=3+2
		 * a=5  5<11  true sout(5)  a=a+2   a=5+ 2
		 * a=7 ...
		 */
		int a = 1;
		while (a < 11) {
			System.out.println(a);
			//a = a + 2;
			a += 2;//步幅
		}
	}
}

2.3do-while循环【几乎不用】

        语法格式:

        do {
            语句体
        } while(布尔表达式);

        执行流程:首先代码执行到do 然后执行do后面大括号中的语句体,再去判断while后面的布尔表达式如果布尔表达式为false的话,就不再执行do后面的语句体了,相当于循环结束。如果布尔表达式为true,再回到do 去执行后面的语句体,执行完语句体之后,再判断while后面的布尔表达式,如果布尔表达式为true的话,就再执行do后面的语句体,然后再去判断while布尔表达式,直到为false的时候,循环结束了。

public class Demo8 {
	public static void main(String[] args) {
		//打印 3遍的 一枝红杏出墙来
		
		/**
		 * i=0  打印第一遍的一枝红杏出墙来  i++ i=1   while后面的布尔表达式  1 <3 true
		 * 	      打印第二遍的一枝红杏出墙来 i++  i=2   while后面的布尔表达式 2< 3 true
		 *     打印第三遍的一枝红杏出墙来 i++  i=3   while后面的布尔表达式3 <3 false  循环结束
		 * 
		 */
		int i = 0;
		do {
			System.out.println("一枝红杏出墙来");
			i++;
		} while (i < 3);
		
		//do-while循环 和while循环的不同之处?
		//while 先判断 再执行     do-while 先执行再判断
	}
}

2.4for循环【重点】

        语法格式:

        for (表达式1; 布尔表达式2; 表达式3) {
            语句体
        }

        表达式1: 初始化的条件

        表达式2: 终止条件

        表达式3: 循环条件

        执行流程: 首先执行表达式1,再执行表达式2,如果布尔表达式2为true,执行大括号中语句体。然后再执行表达式3,再执行表达式2,再次判断布尔表达式2,直到为false,就不执行语句体。就意味着循环结束。

求1~100的和

public class Demo11 {
	public static void main(String[] args) {
		//求1~100的和
		/**
		 * sum是一个变量
		 *  第一次sum = 0     
		 *  第二次sum = sum(第一次的) + 1
		 *  第三次sum = 0 + 1 + 2
		 *  第四次sum = 0+ 1 + 2  + 3
		 * ...
		 * sum = sum + i
		 */
		
		/**
		 * sum = 0; i=0  0<101 true sum= sum + i=>sum=0+0  i++
		 * i=1  1<101  true sum =sum + i  =>sum = 0 + 1  i++
		 * i=2  2<101 true sum = sum + i =>sum=0+1 + 2  i++
		 * i=3  3<101  true sum=sum+i=>sum= 0+1 + 2 + 3 i++
		 * ...
		 * i=100 100<101 true sum=sum+i=>sum =0 + 1+2+...+ 99  + 100 i++
		 * i=101 101<101 false  大括号不再执行, 代码还要继续往下走。
		 * 执行 sout(sum)
		 * 
		 */
		int sum = 0;
		for (int i = 0; i < 101; i++) {
			sum = sum + i;
			
		}
		System.out.println(sum);
	}
}

2.4嵌套循环【重点难点】

        前提是循环。一个循环里面套另外一个循环。秉承着一个特点: 从外层循环先进入到内层循环,然后 内存循环先结束然后再回到外层循环,再进入到内层循环,内层循环结束,再会带外层循环。

public class Demo13 {
	public static void main(String[] args) {
//		System.out.println("****");
//		System.out.println("****");
//		System.out.println("****");
//		for (int i = 0; i < 4; i++) {
//			System.out.print("*");
//		}
//		for (int i = 0; i < 3; i++) {
//			//****   
//			System.out.println("****");
//		}
		/**
		 * i = 0  0< 3 true  执行大括号中的内层的for循环
		 * 		j=0  0<4 true 	打印1个*  j++
		 * 		j=1  1<4  true  打印1个*  j++
		 * 		j=2 2<4 true    打印1个*  j++
		 * 		j=3  3<4  true  打印1个*  j++
		 * 		j=4  4<4 false 内层for循环结束 立马换行    i++  
		 * i = 1  1<3   true  再次执行大括号中的内层的for循环
		 * 		j=0 0< 4 true  打印1个* j++
		 * 		j=1  1<4  true  打印1个* j++
		 * 		j=2  2<4  true 打印1个* j++
		 * 		j=3  3<4  true  打印1个* j++
		 * 		j=4 4<4 false    内层循环结束  立马换行  i++
		 * i=2 2<3   true  再次执行大括号中的内层的for循环
		 * 		j=0   0< 4  打印1个* j++
		 * 		...
		 * 		j=3  3<4  true  打印1个*  j++
		 * 		j=4  4<4 false    内层循环结束  立马换行 i++
		 * i=3 3<3 false  整体循环就结束了
		 */
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 4; j++) {//打印的是4个*
				System.out.print("*");
				
			}
			System.out.println();//换行
		}
		//总结:   最外层for循环 控制的是   行      最内层控制的 是 列
	}
}
public class Demo14 {
	public static void main(String[] args) {
		//打印直角三角形
		
		/**
		 * i = 1  1<5 true 执行大括号内的内层的for循环
		 * 		j=1 1<=1  true 打印1个*  j++
		 * 		j=2 2<=1 false 立马换行   i++
		 * i = 2  2<5  true 执行大括号内的内层的for循环
		 * 		j=1 1<= 3  true  打印*  j++
		 * 		j=2 2<= 3 true   打印一个* j++
		 * 		j=3 3<=3  true   打印一个* j++
		 * 		j=4 4<=3 false 内层循环结束  换行 i++
		 * i= 3 3<5  true 执行大括号内的内层的for循环
		 * 		j=1  1<=5 true  打印一个* j++
		 * 		...
		 * 		j=4  4<=5 true  打印一个* j++
		 * 		j=5  5<=5 true  打印一个* j++		
		 * 		j=6  6<=5  false  内层循环结束  换行 i++
		 * i= 4 4<5  true 执行大括号内的内层的for循环
		 * 		j=1 1<=7  true  打印一个*  j++
		 * 		...
		 * 		j=7 7<=7 true 打印一个*   j+=
		 * 		j=8 8<= 7false  内层循环结束  换行 i++
		 * i=5 5<5  false 整体循环结束
		 * 		
		 */
		for (int i = 1; i < 5; i++) {
			for (int j = 1; j <= (2 * i) - 1; j++) {
				System.out.print("*");
			}
			System.out.println();//换行
		}
	}
}

3.方法【重点】

3.1为什么要有方法

        开发中如果出现了重复的代码或者重复的功能,可以使用循环。但是有的时候循环也满足不了咱们的

        ①代码是臃肿的

        ②代码的维护性极差的

        ③可读性极差

        所以借助于方法来写功能

3.2定义方法的语法格式

        ①无参无返回值的方法

        ②有参无返回值的方法

        ③无参有返回值的方法

        ④有参有返回值的方法

3.2.1无参无返回值的方法

        语法格式:

        public static void 方法的名字 () {
                 语句体
        }

        注意事项:

        ①方法的声明必须放在类中,main方法的外面

        ②方法声明完以后,一定要记得在main函数中调用

练习: 写一个方法,要求 打印一下九九乘法表,使用 无参无返回值的方法

public class Demo7 {
	
	public static void main(String[] args) {
		print99();
	}
	public static void print99 () {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + j*i + "\t");
			}
			System.out.println();
		}
		
	}
	
	
}

3.2.2有参无返回值的方法

        语法格式:

                public static void 方法的名字 (数据类型 变量1, 数据类型 变量2,... ) {
                         语句体
                }

public class Demo8 {
	public static void main(String[] args) {
		//34是实参  实际参数
		printNum(34);
		printHello(8);
		//printHello("goudan");
		printStr("孤舟蓑笠翁,独钓寒江雪");
		
		printNStr(9, "已是黄昏独自愁");
	}
	public static void  printNum (int sb) {//int  num  是形参  形式参数
		System.out.println(sb);
	}
	
	//打印n遍的hello  world
	public static void printHello (int num) {
		for (int i = 0; i < num; i++) {
			System.out.println("hello world");
		}
	}
	//打印随便字符串  打印8遍
	public  static void printStr(String str) {
		for (int i = 0; i < 8; i++) {
			System.out.println(str);
		}
	}
	//打印n遍的随便的字符串
	public static void printNStr (int a, String str) {
		for (int i = 0; i < a; i++) {
			System.out.println(str);
		}
	}
	
}

3.2.3无参有返回值的方法

        语法格式:

                public static  数据类型 方法的名字() {
 
                         return 返回的数据;//返回的值数据类型一定是声明方法的时候 的数据类型是一致的
                }

public class Demo9 {
	public static void main(String[] args) {
		int a = giveNum();//  会返回一个值  5  将返回的数据 给变量 a
		System.out.println(a);
		
		
		System.out.println(giveString());//可以将返回的值  直接打印的
		//System.out.println(str);
		
	}
	//声明一个无参有返回值的方法
	public  static int giveNum () {
		
		return 5;//返回一个5 这个整型的数据
	}
	
	//声明一个返回值是字符串类型的数据的方法
	public static String giveString () {
		String str = "呵呵 爽啊 ";
		str += "xixi";
		return str;
	}
	
	
}

3.2.4有参有返回值的方法

        语法格式:

                public  static 数据类型  方法的名字 (数据类型 变量1, 数据类型 变量2,...) {
                     return  返回的数据;
                }

public class Demo10 {
	public static void main(String[] args) {
		
		int sum = add(3, 4);
		System.out.println(sum);
	}
	//声明一个有参有返回值的方法
	//求两个整数的和
	public static int  add(int a , int b) {
		return a + b;
	}
}

案例: 控制台输入两个int类型的数据,然后将最大值输出

import java.util.Scanner;

public class Demo11 {
	
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入第一个整数:");
		int a = scanner.nextInt();
		System.out.println("请输入第二个整数:");
		int b = scanner.nextInt();
		if (a >= b) {
			System.out.println("两个数中最大的数为:"+a);
		} else {
			System.out.println("两个数中最大的数为:"+ b);
		}
		
	}
}

4.break和continue

4.1break

        只能在swicth-case中或者 循环中 其他地方不能用!!!

public class Demo16 {
	public static void main(String[] args) {
		//break
		//break cannot be used outside of a loop or a switch
		
		for (int i = 0; i < 5; i++) {
			System.out.println("嘻嘻哒");
			break;//终止
		}
		
		for (int i = 0; i < 8; i++) {
			switch (7) {
			case 1:
				System.out.println("哈哈");
				break;//这个break打断的是switch -case
			case 7:
				System.out.println("呵呵");
				break ;
			default:
				System.out.println("jcdsjnjn");
				break;
			}
		}
		
	}
}

4.2continue

        循环中结束本次循环,进入到下次循环

public class Demo17 {
	public static void main(String[] args) {
		/**
		 * i=4 4<=7? true  4==5 false  sout(4) i++
		 * i=5 5<=7 true  5==5 true continue  终止当前的循环,然后进入到下一次循环 i++
		 * i=6 6<=7  true 6==5 false sout(6)
		 */
		for (int i = 4; i <= 7; i++) {
			if (i== 5) {
				continue;
			}
			System.out.println(i);
		}
	}
}

案例:写一个方法,判断一个字符是否是英文小写字符

import java.util.Scanner;

public class Demo18 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符:");
		char x = scanner.next().charAt(0);
		test(x);
		
		
	}
	public static void test (char ch1) {
		//  char类型的数据 十进制的ASSIC码值的
//		if (ch1 >= 'a' && ch1 <= 'z') {
		if (ch1 >= 97 && ch1 <= 122) {
			System.out.println("是英文的小写字符");
		} else {
			System.out.println("不是英文的小写字符");
		}
	}
}

5.数组【重点】

5.1开发中为啥要使用数组

        如果开发中出现了大量的同一个类型的数据,按照现在所学的知识点,声明变量的话。如果一个变量存一个数据的话,那么就会需要多个变量了,相当麻烦。

        使用数组: 只需要一个变量,然后数组中存很多的数据,把数组想成 一个容器。如:int[] arr = {1,2,3,4,5,6};

5.2数组在Java中如何定义的

        第一种:

                数据类型[]   数组的名字 = {值1, 值2, 值3,...};
                或
                数据类型   数组的名字 []= {值1, 值2, 值3,...};

        第二种:

                数据类型[]   数组的名字 = new  数据类型[容量];
                或        
                数据类型 数组的名字[] = new  数据类型[容量];

        第一种和第二种区别: 第一种即声明了数组又对数组赋值了,第二种只是声明空的数组而已,暂时还没有赋值。

        第三种(用的很少):

                数据类型[] 数组的名字  = new 数据类型[]{值1, 值2, 值3,...};

import java.util.Arrays;

public class Demo4 {
	public static void main(String[] args) {
		//数组第一种声明方式 :数据类型[]   数组的名字 = {值1, 值2, 值3,...};
		int[] arr = {23, 34, 12, 15};
		System.out.println(arr);//[I@15db9742  内存地址   但是数据看不到 那咋办?
		System.out.println(Arrays.toString(arr));//  将一个内存地址形式的数据变成 人类可以看懂的
		//[23, 34, 12, 15]
		
		String[] strs = {"狗蛋","文博", "广瑞"}; 
		System.out.println(strs);//@6d06d69c
		System.out.println(Arrays.toString(strs));//[狗蛋, 文博, 广瑞]
		
		boolean[] bools = {true, true, false, true};
		System.out.println(bools);//[Z@7852e922
		System.out.println(Arrays.toString(bools));//[true, true, false, true]
		
		float[] f2 = {12.3f, 45.6F};
		
		
		//声明一个char类型的数组
		char[]  chs = {'a', '中', '9'};
		System.out.println(chs);//a中9  这个比较特殊 打印不是地址  是数组中值组成的
		
	}
}

5.3第二种声明方式 是空的数组,如何赋值

import java.util.Arrays;

public class Demo7 {
	public static void main(String[] args) {
		//第二种数组的声明方式
		int[] arr = new int[3];//arr = {0,0,0};
		//对这个空的数组进行的的赋值
		arr[0] = 23;//将23d赋值给 下标为0 的arr这个数组  arr = [23, 0, 0]
		//System.out.println(Arrays.toString(arr));
		arr[1] = 45;
		//System.out.println(Arrays.toString(arr));//arr = [23, 45, 0]
		arr[2] = 678;
		System.out.println(Arrays.toString(arr));//arr = [23, 45, 678]
		//arr[3] = 250;  这个下标越界
		//ArrayIndexOutOfBoundsException   数组下标越界
		//System.out.println(Arrays.toString(arr));
		
		
		System.out.println("=========");//arr = [23, 45, 678]
		/**
		 * i=0  0<3  true   arr[0] = 89   arr = [89, 45, 678] i++
		 * i=1 1<3  true  arr[1] = 90 arr=[89, 90, 678] i++
		 * i=2 2<3  true  arr[2]= 91  arr =[89, 90, 91] i++
		 * i=3 3<3 false  循环结束以后  arr =[89, 90, 91]
		 */
		for (int i = 0; i < 3; i++) {
			arr[i] = 89 + i;
		}
		
		System.out.println(Arrays.toString(arr));//[89, 90, 91]
		
	}
}

5.4对数组进行取值

        通过下标来取值

public class Demo8 {
	public static void main(String[] args) {
		int[] arr = {32, 45, 89, 12, 999, 5667};
		//获取数组中的值
		System.out.println(arr[0]);//32
		System.out.println(arr[1]);//45
		System.out.println(arr[2]);//89
		System.out.println(arr[3]);//12
		System.out.println("==============");
		
		for (int i = 0; i < 4; i++) {
			//i的循环的值 刚好作为数组的下标
			System.out.println(arr[i]);
		}
		
		//数组.length    数组的长度
		System.out.println(arr.length);//  arr的长度    4
		
		System.out.println("---------------");

		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		
		
		
		String[] strs = {"张三", "狗蛋", "麻子", "嘻嘻"};
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
		
	}
}

5.5二维数组【了解】

        语法格式:

                数据类型[][]  数组名字 = new 数据类型[容量][荣量];

import java.util.Arrays;

public class Demo9 {
	public static void main(String[] args) {
		int[][] arr = new int[2][3];
		
		arr[0][0] = 78;
		arr[0][1] = 89;
		arr[0][2] = 100;
		arr[1][0] = 250;
		arr[1][1] = 101;
		arr[1][2] = 99;
		
		//第一个[]  是行  第二个[] s是列
		//取数据  使用循环来取
		for (int i = 0; i < 2; i++) {//控制行
			for (int j = 0; j < 3; j++) {//控制列
				
				System.out.println(arr[i][j]);
			}
		}
		
		
		
	}
}

5.6数组可以 作为方法的参数

public class Demo10 {
	public static void main(String[] args) {
		int[] arr1 = {1,2,3,4};
		printArr(arr1);
	}
	//方法 的参数  int  a ,   String str   , int[] arr;
	public static void printArr (int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
	//在main主函数中 声明一个数组变量  这个数组有值,  然后通过方法 printArr 将数组的值(arr1)传递
	//给形参(arr) ,然后在方法中 将arr通过for循环进行遍历
}

5.7数组可以作为方法的返回值

import java.util.Arrays;

public class Demo14 {

	public static void main(String[] args) {
		int[] arr = {9, 8, 7, 6};
		int[] arr2 = reverse(arr);
		System.out.println(Arrays.toString(arr2));
	}
	{1,2,3,4}  =>  {4,3,2,1}
	public static int[] reverse (int[] arr) {
		int[] arr1 = new int[arr.length];//创建了一个空的数组 长度 和 arr一样的
		for (int i = 0, j = arr.length - 1; i < arr.length; i++, j--) {
			arr1[j] = arr[i];
		}
		return arr1;
	}
}

案例:需求: 定义一个方法,找出来一个int数组中最大的那个值的下标

public class Demo16 {
	public static void main(String[] args) {
		
		/**
		 * 1.arr = {1,23,3,1}
		 * 2.执行getMaxIndexInArray 方法  去看下面的自己写的方法
		 * 3. maxIndex = 0 变量
		 * 4.碰到一个for循环
		 * 	分析for循环的执行的流程:
		 * 		i=1 1<4 true  if 语句 arr[0] < arr[1]  1<23 true  maxIndex = 1 i++
		 * 		i=2 2<4  true  if arr[1]<arr[2]  23 < 3 false maxIndex = 1 i++
		 * 		i=3 3<4 true  if arr[1] <arr[3] 23 <1 false  maxIndex = 1 i+=
		 * 		i=4 4<4 bfalse  
		 * 		return  maxIndex = 1; 
		 * 5.返回的是1    将1赋值给a了
		 * 6. 打印了a  得到结果是1
		 * 
		 */
		int[] arr = {1,23,3,1};
		int a = getMaxIndexInArray(arr);
		System.out.println(a);
	}
	//定义一个方法,找出来一个int数组中最大的那个值的下标  有参 有返回值
	public static int getMaxIndexInArray (int[] arr) {
		int maxIndex = 0;//接最大值的下标
		for (int i = 1; i < arr.length; i++) {
			if (arr[maxIndex] < arr[i]) {
				maxIndex = i;
			}
		}
		return maxIndex;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值