#码神心得_06# 运算符、循环流程控制、数组

这一次课的内容挺多的,虽然以前基本都有学过,但是“温故而知新”,收获也是蛮大的!


一、运算符

1、算术运算符:+、-、*、/、%等

2、关系运算符:>=、<=、==、===(恒等于)

3、逻辑运算符:&&、||、!、(与或非)

4、赋值运算符:==、+=、-=等

5、位运算符:针对2进制数

       & 与:同时为1返回1

       | 或:只要有1返回1

       ~ 非:翻转所有位,一元运算

       ^ 异或:只要不相同返回1

       << 左移:向左移动2位,向右补0

       >>右移:向右移动2位,向左补符号位

      >>>无符号位右移:向右移动2位,向左补0

6、三目运算符:a>b?a:b

7、其他运算符:对象运算符instanceof,判断该对象是否为指定对象,返回true或false


二、循环流程控制

一般情况下,三种循环结构都可以互相转换

1、while:

2、do-while

3、for

4、foreach:在遍历数组、集合方面使用较方便

		int[] temp = {3,7,9,6,12,4,30,45,20,89};
		
		for (int i : temp) {
			System.out.print(i+" ");
		}


5、关键字:

        break:终止整个循环

       continue:跳过本次循环

       return:跳出这个方法


三、数组

1、数组的声明:静态声明:int[] a = {1,2,3,4,5};       动态声明:int[] a = new int[5];只声明数组长度

2、数组的遍历:使用循环结构,由数组下标进行遍历:

		int[] temp = {3,7,9,6,12,4,30,45,20,89};
		
		for (int i = 0; i < temp.length; i++) {
			System.out.print(temp[i]+" ");
		}
3、数组里的数据类型不是绝对要一致,例如声明一个对象数组,里面的对象可以是一个父类的不同的子类对象。

4、数组出错类型:

(1)超出界限j:ava.lang.ArrayIndexOutOfBoundsException     

(2)内存溢出:exceeds VM limit     

(3)数组元素个数超出最大值:Requested array size

5、Arrays类的一些用法:

(1)查找数组元素的下标:int index = Arrays.binarySearch(目标数组, 目标元素);

(2)复制数组:int[] temp = Arrays.copyOf(目标数组,长度);

(3)输出数组:Arrays.toString(目标数组);

(4)改变数组元素:Arrays.fill(temp, 3,6, 100);将数组temp下标3—6的元素都改为100

(5)对数组进行升序排序:Arrays.sort(temp);


课堂作业

作业一:使用循环语句输出九九乘法表

public class MultiplicationTable {
	public static void main(String[] args) {
		
		String result ;
		for (int i = 1; i < 10; i++) {
			for (int j = 1; j <= i; j++) {
				
				result = i + "*" + j + "=" + (i * j);

				if (i == j) {
					System.out.print(result);
				} else {
					System.out.print(result + ",");
				}

			}
			System.out.println();
		}
	}

}
效果截图:



作业二:使用循环输出等腰三角形

public class Triangle {

	public static void main(String[] args) {
		System.out.println("请输入等腰三角形高:");
		Scanner scanner = new Scanner(System.in);
		int length = scanner.nextInt();
		
		int temp = 0;
		for (int i = 0; i < length; i++) {
			for (int j = 0; j < length * 2 - 1; j++) {
				if (j + 1 < (length - temp) || j + 1 > (length + temp)) {
					System.out.print(" ");
				} else {
					System.out.print("*");
				}

			}
			temp++;
			System.out.println();
		}

	}

}
效果截图:


作业三:请将下面的数组去重,并输出最后结果,可能的话封装成一个通用的方法

 String [] str = {“Java”, “C++”, “Php”, “C#”, “Python”, “C++”, “Java”};

public class DuplicationRemove {

	public static void main(String[] args) {

		String[] str = { "Java", "C++", "Php", "C#", "Python", "C++", "Java" };

		DuplicationRemove du = new DuplicationRemove();
		String[] result = du.removeDuplication(str);
		System.out.println("原数组为:" + Arrays.toString(str));
		System.out.println("\n去重后为:" + Arrays.toString(result));

	}

	public String[] removeDuplication(String[] str) {
		String tempstr = "";

		for (int i = 0; i < str.length; i++) {
			int temp = 0;
			for (int j = 0; j < i; j++) {
				if (str[i].equals(str[j])) {
					temp = 1;
					break;
				}
			}
			if (temp == 0) {
				tempstr += str[i] + " ";
			}
		}
		String[] result = tempstr.split(" ");
		return result;
	}

}
效果截图:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值