java--(二)运算符、表达式和语句

自增,自减运算符

++x--x 表示在使用x之前,先使x的值增(减)1
x++x--表示在使用x之后,使x的值增(减)1
例:x=5
y=++x; 输出 y=6
   先执行x=x+1再使用x的值
y=x++ 输出 y=5
   先执行x的值再执行 x=x+1

三元运算符

有三个参数,第一个是条件表达式,其余两个是值,条件表达式成立时运算取第一个值,不成立时取第二个值

public class Calculation3_26 {
	public static void main(String[] args) {
		int i = 46;
		int j = 97;
		int z = i > j ? 100: 200;
		System.out.println("i>j?100:200的值是: "+z);
	}
}

在这里插入图片描述

关系运算符

> 大于
< 小于
>= 大于等于
<= 小于等于
== 等于
!= 不等于

逻辑运算符

&&
||
!

位运算符

一个int型在内存中占4个字节共32位
int 型数据7 的二进制 0111
  00000000 00000000 00000000 00000111
左面最高位是符号位,最高位是0表示正数,是1表示负数,负数采用补码表示
比如 -8 二进制1000
  11111111 11111111 11111111 11111000

按位 与运算 &, 同真为真,其余为假
   对两个整型数据a, b 按位进行运算,运算结果是一个整型数据 c.
   如果b的精度高于a,那么结果c 的精度和b相同
   如果 a, b两个数据对应位都是1, 则c的该位是1,否则是0
按位 或运算 |,同假为假,其余为真
   对两个整型数据a, b 按位进行运算,运算结果是一个整型数据 c.
   如果b的精度高于a,那么结果c 的精度和b相同
   如果 a, b两个数据对应位都是0, 则c的该位是0,否则是1
按位 非运算 ~
   对两个整型数据a, b 按位进行运算,运算结果是一个整型数据 c.
   如果 a对应位是0, 则c的该位是1,否则是0
按位 异或运算 ^
   对两个整型数据a, b 按位进行运算,运算结果是一个整型数据 c.
   如果b的精度高于a,那么结果c 的精度和b相同
   如果 a, b两个数据对应位相同, 则c的该位是0,否则是1
   a^a=0, a^0=a
   如果c=a^b, 那么 a=c^b, 也就是说, ^的逆运算仍然是^a^b^b=a

public class Example3_1 {
	public static void main(String args[]) {
		char a1='十', a2='点', a3='进', a4='攻';
		char secret = 'A';
		a1 = (char)(a1^secret);
		a2 = (char)(a2^secret);
		a3 = (char)(a3^secret);
		a4 = (char)(a4^secret);
		System.out.println("密文:"+a1+a2+a3+a4);
		a1 = (char)(a1^secret);
		a2 = (char)(a2^secret);
		a3 = (char)(a3^secret);
		a4 = (char)(a4^secret);
		System.out.println("原文:"+a1+a2+a3+a4);
	}
}

在这里插入图片描述

不用其他变量实现两变量互换

import java.util.Scanner;
public class VariableExchange {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入变量A的值");
		long A = scan.nextLong();
		System.out.println("请输入变量B的值");
		long B = scan.nextLong();
		System.out.println("A="+A+"\tB="+B);
		System.out.println("执行变量互换...");
		A = A ^ B;
		B = B ^ A;
		A = A ^ B;
		System.out.println("A="+A+"\tB="+B);
	}
}

在这里插入图片描述

instanceof 运算符

该运算符是二目运算符,左面的操作是一个对象,右面是一个类。
当左面的对象是右面的类或子类创建的对象时,该运算符运算的结果是true,否则是false

if条件分支语句

例题:将变量a,b,c 中的数值按大小,从小到大顺序进行互换

public class Example3_2 {
	public static void main(String args[]) {
		int a=9, b=5, c=7, t=0;
		if (b<a) {
			t = a;
			a = b;
			b = t;
		}
		if (c<a) {
			t = a;
			a = c;
			c = t;
		}
		if (c<b) {
			t = b;
			b = c;
			c = t;
		}
		System.out.println("a="+a+",b="+b+",c="+c);
	}
}

在这里插入图片描述

public class Example3_3 {
	public static void main(String args[]) {
		int math = 65, English = 85;
		if (math > 60) {
			System.out.println("数学及格了");
		}
		else {
			System.out.println("数学不及格");
		}
		if (English > 90) {
			System.out.println("英语是优");
		}
		else {
			System.out.println("英语不是优");
		}
		System.out.println("我在学习if-else语句");
	}
}

在这里插入图片描述

else if 语句

public class Example3_3_2 {
	public static void main(String args[]) {
		int score = 85;
		if (score > 90) {
			System.out.println("奖励一个IPone 5S");
		}
		else if (score > 70) {
			System.out.println("奖励一个红米");
		}
		else {
			System.out.println("惩罚500个俯卧撑");
		}
	}
}

在这里插入图片描述

switch 开关语句

switch语句种表达式的值可以是byte,short,int, char型,但不可以是long型数据

import java.util.Scanner;
public class Example3_4 {
	public static void main(String args[]) {
		int number = 0;
		System.out.println("输入正整数(回车确定)");
		Scanner reader = new Scanner(System.in);
		number = reader.nextInt();
		switch(number) {
			case 9:
			case 131:
			case 12: System.out.println(number + "是三等奖");
					break;
			case 209:
			case 596:
			case 27: System.out.println(number + "是二等奖");
					break;
			case 875:
			case 316:
			case 59: System.out.println(number + "是一等奖");
					break;
			default: System.out.println(number + "未中奖");
		}
	}
}

在这里插入图片描述

for循环语句

计算 8+88+8888+… 的前12项和

public class Example3_5 {
	public static void main(String args[]) {
		long sum =0, a=8, item =a, n=12, i=1;
		for (i=1;i<=n;i++) {
			sum = sum + item;
			item = item*10+a;
		}
		System.out.println(sum);
	}
}

在这里插入图片描述

while 循环

计算 1 + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! . . . 1+1/2!+1/3!+1/4! ... 1+1/2!+1/3!+1/4!...前20项和

public class Example3_6 {
	public static void main(String args[]) {
		double sum = 0, item = 1;
		int i=1, n=20;
		while(i<=n) {
			sum = sum + item;
			i= i+1;
			item = item*(1.0/i);
		}
		System.out.println("sum="+sum);
	}
}

在这里插入图片描述在这里插入图片描述

do while 语句

do_while 与 while 循环的区别是 do-while 的循环体至少被执行一次
如上例子: 1 + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! . . . 1+1/2!+1/3!+1/4! ... 1+1/2!+1/3!+1/4!...前20项和

public class Example3_6_2 {
	public static void main(String args[]) {
		double sum = 0, item = 1;
		int i=1, n=20;
		do {
			sum = sum + item;
			i= i+1;
			item = item*(1.0/i);
		}while(i<=n);
		System.out.println("sum="+sum);
	}
}

在这里插入图片描述

break和continue 语句

某次循环执行了break语句,那么整个循环语句就结束了
某次循环执行了continue语句,那么本次循环就结束,即不再执行本次循环中循环体中continue语句后面的语句,而转入进行下一次循环

public class Example3_7 {
	public static void main(String args[]) {
		int sum=0,i,j;
		//计算 1+3+5+7+9
		for(i=1;i<=10;i++) {
			if(i%2==0) {
				continue;
			}
			sum=sum+i;
		}
		System.out.println("sum="+sum);
		// 求100内的素数
		for (j=2;j<=100;j++) {
			for (i=2;i<=j/2;i++) {
				if(j%i==0)
					break;
			}
			if(i>j/2) {
				System.out.println(""+j+"是素数");
			}
		}
	}
}

在这里插入图片描述

for语句与数组

for (声明循环变量 : 数组的名字){
}
for(声明循环变量:数组的名字)中的“声明循环变量”必须是变量声明,不可以使用已经声明过的变量

public class Example3_8 {
	public static void main(String args[]) {
		int a[] = {1,2,3,4};
		char b[] = {'a','b','c','d'};
		//传统方法
		for (int n=0; n < a.length; n++) {
			System.out.println(a[n]);
		}
		for (int n=0; n < b.length;n++) {
			System.out.println(b[n]);
		}
		// 改进方法,循环变量i依次取数组a的每一个元素的值
		for (int i:a) {
			System.out.println(i);
		}
		for (char ch:b) {
			System.out.println(ch);
		}
	}
}

举例

例题:用户依次输入若干个数字,输入后按回车确认,最后在键盘上输入一个非数字字符串结束操作

import java.util.*;
public class Example3_9 {
	public static void main(String args[]) {
		Scanner reader = new Scanner(System.in);
		double sum = 0;
		int m = 0;
		while(reader.hasNextDouble()) {
			double x = reader.nextDouble();
			m = m+1;
			sum = sum +x;
		}
		System.out.printf("%d个数的和为%f\n",m,sum);
		System.out.printf("%d个数的平均值是%f\n",m,sum/m);
	}
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值