实训二 第二天
1,转义字符
\n 换行 \r 回车 \t 水平制表 \ ' ' 单引号 \" 双引号 \\ 斜杆
2, 运算符
运算符包括:算术,赋值,关系,逻辑,位,移位,连接,三目等运算符
算术运算符:+ 、 - 、 * 、 / 、 % 、 ++ 、 -赋值运算符:= 、 += 、 -= 、 *= 、 /= 、 %= 关系运算符:> 、 < 、 >= 、 <= 、 == 、 != 逻辑运算符:! 、 & 、 | 、 ^ 、 && 、 || 位运算符:& 、 | 、 ^ 、 ~ 、 >> 、 << 、 >>> 字符串连接运算符:+
算术运算符
++:如果是前缀:先对此变量加 1,再执行其他的操作。 如果是后缀:先执行其他的操作,再对此变量加 1 --:同理。
键盘输入:Scannerinput=newScanner(System.in);
赋值运算符
关系运算符
关系运算符作用是比较两边的操作数,结果总是 boolean 型的。
逻辑运算符
逻辑运算符用于对 boolean 型结果的表达式进行运算,运算结果总是 boolean 型,后面结合条件结构讲解。
位运算符
位运算符对两个操作数中的每一个二进制位都进行运算
位运算符功能 按位取反 ~ 按位与 & 按位或 | 按位异或 ^
位移运算符
左移:"a<<b;"将二进制形式的 a 逐位左移 b 位,最低位空出的 b 位补 0 带符号右移:"a>>b;"将二进制形式的 a 逐位右移 b 位,最高位空出的 b 位补原来的符号位 无符号右移:"a>>>b;"将二进制形式的 a 逐位右移 b 位,最高位空出的 b 位补 0
2227= 00000000 00000000 00001000 10110011
2227<<3= 00000000 00000000 01000101 10011000
2227>>3= 00000000 00000000 00000001 00010110
2227>>>3= 00000000 00000000 00000001 00010110
-2227= 11111111 11111111 11110111 01001101
-2227<<3= 11111111 11111111 10111010 01101000
-2227>>3= 11111111 11111111 11111110 11101001
-2227>>>3= 00011111 11111111 11111110 11101001
字符串连接运算符
Strings="He"+"llo"; 结果"Hello" "+"除了可用于字符串相连接,也能将字符串与其它的数据类型相连成一个新的字符串。 如:Strings="x"+123; 结果"x123"
三目运算符
X?Y:Z X 为 boolean 类型表达式,先计算 x 的值,若为 true,整个三目运算的结果为表达式 y 的值,否则整个运算结果为表达式 z的值。
例: intscore=75; Stringtype=score>=60?"及格":"不及格";
表达式和语句
条件语句
if 条件结构是根据条件判断之后再做处理 if(条件语句){…} if(条件语句){…}else{…} if(条件语句){…}elseif(条件语句){…} if(条件语句){…}elseif(条件语句){…}else{…}
switch switch(表达式){
case 取值 1: 语句块 1;break; case 取值 n: 语句块 n;break; default: 语句块 n+1;break; }
switch语句有关规则 表达式的返回值必须是下述几种类型之一:int,byte,char,short,确定的结果; case 子句中的取值必须是常量,且所有 case 子句中的取值应是不同的; default 子句是可选的; break 语句用来在执行完一个 case 分支后使程序跳出 switch 语句块;如果 case 后面没有写 break 则直接往下面执行! Case 后面的执行体可写{}也可以不写{}
class Demo1 {
public static void main(String[] args) {
//代码是从上至下,从左至右一次执行的.
int a = 5;
++a;
System.out.println(a);
//System.out.println(5);
System.out.println(a);
/*
int a = 10;
int b = 3;
System.out.println(a%b);
*/
}
}
class Demo2 {
public static void main(String[] args) {
int a = 5;
a+=3;// a = a + 3;
System.out.println(a);
int x = 10;
int y = 3;
x-=y;//x = x - y;
System.out.println(x);
}
}
class Demo3 {
public static void main(String[] args) {
int x = 7;
x+=x++ + ++x;
/*
x+=x++ + ++x;
x = x + [x++ + ++x]
7 = 7 + [7 + 9]
7 = 7 + 16
23
*/
System.out.println(x);
}
}
import java.util.Scanner;
/*
Java接收用户键盘输入
1. 导包: import java.util.Scanner;
2. 实例化 Scanner 对象
Scanner input = new Scanner(System.in);
3. 接收用户的输入
1> 接收整型: input.nextInt();
2> 接收Double:input.nextDouble();
3> 接收String:input.nextString();
*/
class Demo4 {
public static void main(String[] args) {
//2. 实例化 Scanner 对象
Scanner input = new Scanner(System.in);
//3. 开始接收用户输入
System.out.print("请输入您的年龄:");
int age = input.nextInt();
System.out.println("您的年龄是:" + age);
System.out.print("请输入您的身高(单位厘米):");
int height = input.nextInt();
System.out.println("您的身高是:" + height);
}
}
class Demo5 {
public static void main(String[] args) {
System.out.println(!true);
/*
true ^ true = false
true ^ false = true
false ^ true = true
false ^ false = false
^:左右两边的表达式运算结果不同,^的运算结果为true
左右两边的表达式运算结果相同,^的运算结果为false
true | true = true
true | false = true
false | true = true
false | false = false
|:全假则假
左右两边的表达式运算结果都为 false 的时候,|运算的结果才为false
左右两边的表达式只要有一个表达式的运算结果为true,那么|运算的结果就为true
||:
与 | 运算原理基本相同,如果左边的表达式运算结果为true,||右边的表达式不参与运算.
true & true = true
true & false = false
false & true = false
false & false = false
&运算:全真则真,
左右两边的表达式,运算结果都为 true 的时候 &运算的结果才为 true
左右两边的表达式,只要有一个表达式的运算结果为false,那么&运算的结果就为false.
false && x<y
& 和 && 的区别
&&和&的运算原理基本相同,在左边的表达式运算结果为false的时候.&&右边的表达式不参与运算.
*/
/*
int a = 5;
int b = 5;
System.out.println(a != b);
*/
}
}
class Demo6 {
//Java 是一门强类型的语言,定义什么类型的变量就要赋什么类型的值.
public static void main(String[] args) {
String str = "Hello" + " QiDi" + " !!!";
System.out.println(str);
System.out.println("这个字符串的值是:" + str);
}
}
import java.util.Scanner;
/*
接收用户输入的成绩:
判断成绩是否及格
score >= 60
*/
class Demo7 {
public static void main(String[] args) {
/*
a+b
X ? Y : Z
*/
//实例化 Scanner 对象 对象名叫 input
Scanner input = new Scanner(System.in);
//提示并接收用户输入的成绩
System.out.print("请输入您的成绩:");
//score 存储用户的成绩
int score = input.nextInt();
//判定成绩是否及格
String result = score>=60 ? "及格" : "不及格";
System.out.println("您输入的成绩:" + result);
}
}
class HelloWorld {
public static void main(String[] args) {
System.out.println("白哥\r真的\\真的\"好帅\"啊!");
}
}
/*
if(条件语句){…}
if (条件语句){…}else{…}
*/
class IfDemo1 {
public static void main(String[] args) {
//if(条件语句){…}
int a = 10;
if(a>2){//userID==11000110000 password==123456
//业务逻辑处理代码......
System.out.println("这个数比2大");
}else{
//当if括号内的条件不成立的时候,需要执行的业务逻辑处理
System.out.println("这个数比2小");
}
//....继续执行后面的代码
System.out.println("Hello World");
}
}
/*
if (条件语句){…}else if(条件语句){…}
if (条件语句){…}else if(条件语句){…}else{…}
*/
class IfDemo2 {
public static void main(String[] args) {
//会员卡的会员状态
//1.普通会员 2. 银卡会员 3.金卡会员 4.铂金会员
int cardStauts = 6;
if (cardStauts == 1){
System.out.println("尊贵的会员,您是普通会员,全场消费98折");
//业务逻辑处理代码
}else if (cardStauts == 2){
System.out.println("尊贵的会员,您是银卡会员,全场消费9折");
}else if (cardStauts == 3){
System.out.println("尊贵的会员,您是金卡会员,全场消费8折");
}else if (cardStauts == 4){
System.out.println("尊贵的会员,您是铂金还有,全场消费5折");
}else{
//上面罗列的条件都不满足,这里做一个统一的业务逻辑处理
System.out.println("您还不是本店的会员,建议您加入会员,享受全场消费折扣");
}
}
}
import java.util.Scanner;
/*
需求:接收用户输入的成绩,根据成绩判定 优秀 良好 中等 不及格
优秀 > 90
良好 80 -- 90
中等 60 -- 80
不及格 < 60
*/
class IfTest1 {
public static void main(String[] args) {
//实例化 Scanner 对象
Scanner input = new Scanner(System.in);
//提示用户输入成绩,并且用变量接收存储
System.out.print("请输入您的成绩:");
int score = input.nextInt();
//对成绩进行判定
if(score>=0 && score <= 100){
//如果条件成立,代表用户输入的数据合法
//就可以进行下面的业务逻辑处理
if (score>=90){
System.out.println("恭喜!您的成绩为优秀!");
}else if (score >= 80){
System.out.println("恭喜!您的成绩为良好!");
}else if (score >= 60){
System.out.println("恭喜!您的成绩为中等!");
}else{
System.out.println("您的成绩不及格!请准备补考费吧!");
}
}else{
//数据输入不合法
System.out.println("对不起,您输入的成绩超出范围,请检查后重新输入!");
}
}
}
/*
switch(表达式){
case 取值 1: 语句块 1;
break;
case 取值 n: 语句块 n;
break;
default: 语句块 n+1;
break;
}
*/
class SwitchDemo1 {
public static void main(String[] args) {
//会员卡的会员状态
//1.普通会员 2. 银卡会员 3.金卡会员 4.铂金会员
int cardStatus = 30;
switch (cardStatus){
case 1:
System.out.println("您的卡为普通会员");
break;
case 2:
System.out.println("您的卡为银卡会员");
break;
case 3:
System.out.println("您的卡为金卡会员");
break;
case 4:
System.out.println("您的卡为铂金会员");
break;
default:
System.out.println("你还未加入会员!");
break;
}
}
}
class SwitchDemo2 {
public static void main(String[] args) {
//会员卡的会员状态
//1.普通会员 2. 银卡会员 3.金卡会员 4.铂金会员
int cardStatus = 1;
switch (cardStatus){
case 1:
System.out.println("您的卡为普通会员");
//break;
case 2:
System.out.println("您的卡为银卡会员");
//break;
case 3:
System.out.println("您的卡为金卡会员");
break;
case 4:
System.out.println("您的卡为铂金会员");
//break;
default:
System.out.println("你还未加入会员!");
//break;
}
}
}
/*
输入月份输出天数
*/
class SwitchTest1 {
public static void main(String[] args) {
int month = 4;
switch (month){
case 2:
System.out.println(month + "月共有28天");
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month + "月共有31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month + "月共有30天");
break;
default:
System.out.println("月份有误!请重新输入!");
break;
}
/*
switch (month){
case 1:
System.out.println(month + "月共有31天");
break;
case 2:
System.out.println(month + "月共有28天");
break;
case 3:
System.out.println(month + "月共有31天");
break;
case 4:
System.out.println(month + "月共有30天");
break;
case 5:
System.out.println(month + "月共有31天");
break;
case 6:
System.out.println(month + "月共有30天");
break;
case 7:
System.out.println(month + "月共有31天");
break;
case 8:
System.out.println(month + "月共有31天");
break;
case 9:
System.out.println(month + "月共有30天");
break;
case 10:
System.out.println(month + "月共有31天");
break;
case 11:
System.out.println(month + "月共有30天");
break;
case 12:
System.out.println(month + "月共有31天");
break;
default:
System.out.println("月份有误!请重新输入!");
}
*/
}
}