一、java运算符
1.1算术运算符
符号 说明 + 加法 - 减法 * 乘法 % 取余 ++ 自增 – 自减 / 求商
1.2赋值运算符
符号 说明 = 等于 += 加等 -= 减等 *= 乘等 /= 除等 %= 取余等于
1.3关系运算符
符号 说明 < 小于 <= 小于等于 > 大于 >= 大于等于 == 等等于 != 不等于
1.4逻辑运算符
1.5位运算符
符号 说明 实例 & 按位与运算(串行电路) 4&5=4 | 按位或运算(并行电路) 4|5=5 ~ 非 ~10=65525 ^ 异或(相同为0,相异为1) 11^7=12
1.6位移运算符
符号 说明 实例 等价于 << 向左位移 (8<<3)=64 8*2^3=64 >> 向右位移 (8>>3)=1 8*2^-3=1 >>> 无符号右移 (8>>>3)=1 数据为正数等价于向右位移,但效率高
1.7三元运算符
条件表达式?成立表达式:未成立表达式 实例:3>2 ? “是的”:“不是的”
二、进制
2.1常用进制
进制名称 进制符号 例子 二进制 ob(B) ob01 八进制 o o15 十进制 无 22 十六进制 ox oxaf
2.2十进制转任意进制
使用短除法,除以该进制直到商为零 ,结果就是从最后一个余数读到第一个
2.3任意进制转成十进制
使用乘权求和,即为:数字*进制(位数-1)+……+数字乘进制 0
三、选择结构
3.1单分支if
package com. la ;
import java. util. Scanner ;
public class If {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入你的成绩" ) ;
int score = scanner. nextInt ( ) ;
if ( score>= 60 ) {
System . out. println ( "及格" ) ;
}
if ( score< 60 ) {
System . out. println ( "不及格" ) ;
}
}
}
3.2if…else
import java. util. Scanner ;
public class If {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入你的成绩" ) ;
int score = scanner. nextInt ( ) ;
if ( score>= 60 ) {
System . out. println ( "及格" ) ;
}
if ( score< 60 ) {
System . out. println ( "不及格" ) ;
}
ifElse ( ) ;
}
public static void ifElse ( ) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入你的成绩" ) ;
int score = scanner. nextInt ( ) ;
if ( score>= 60 ) {
System . out. println ( "及格" ) ;
} else {
System . out. println ( "不及格" ) ;
}
}
}
3.3多分支if
package com. la ;
import java. util. Scanner ;
public class ElseIf {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入你的成绩" ) ;
int score = scanner. nextInt ( ) ;
if ( score>= 80 ) {
System . out. println ( "优秀" ) ;
} else if ( score>= 60 && score< 80 ) {
System . out. println ( "及格" ) ;
} else {
System . out. println ( "未及格" ) ;
}
}
}
3.4嵌套if
package com. la ;
import java. util. Scanner ;
public class NestIf {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入你的成绩" ) ;
int score = scanner. nextInt ( ) ;
if ( score> 60 ) {
System . out. println ( "已及格" ) ;
if ( score>= 80 ) {
System . out. println ( "优秀" ) ;
} else {
System . out. println ( "良好" ) ;
}
} else {
System . out. println ( "未及格" ) ;
}
}
}
四、练习
4.1统计年龄层次比例
package com. la ;
import java. math. BigDecimal ;
import java. util. Scanner ;
public class work1 {
static int count;
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
for ( int i = 1 ; i < 11 ; i++ ) {
System . out. print ( "请输入第" + i+ "的年龄:" ) ;
int age = scanner. nextInt ( ) ;
if ( age< 30 ) {
count++ ;
}
}
BigDecimal a = new BigDecimal ( count) ;
BigDecimal b = new BigDecimal ( 10 ) ;
BigDecimal f = new BigDecimal ( 100 ) ;
BigDecimal d = new BigDecimal ( 1 ) ;
BigDecimal c = a. divide ( b) ;
System . out. println ( count) ;
System . out. println ( ( double ) ( count/ 10 ) ) ;
System . out. println ( "30岁以下的比例:" + c. multiply ( f) + "%" ) ;
System . out. println ( "30岁以上的比例:" + ( d. subtract ( c) ) . multiply ( f) + "%" ) ;
}
}
4.2求商品折扣
package com. la ;
import java. util. Scanner ;
public class work2 {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入商品总价格" ) ;
int totalPrice = scanner. nextInt ( ) ;
double discount;
if ( totalPrice== 550 ) {
discount= 1 ;
System . out. println ( "折扣后总价为:" + totalPrice* discount) ;
} else if ( totalPrice>= 500 ) {
discount= 0.5 ;
System . out. println ( "折扣后总价为:" + totalPrice* discount) ;
} else if ( totalPrice>= 400 && totalPrice< 500 ) {
discount= 0.6 ;
System . out. println ( "折扣后总价为:" + totalPrice* discount) ;
} else if ( totalPrice>= 300 && totalPrice< 400 ) {
discount= 0.7 ;
System . out. println ( "折扣后总价为:" + totalPrice* discount) ;
} else if ( totalPrice>= 200 && totalPrice< 300 ) {
discount= 0.8 ;
System . out. println ( "折扣后总价为:" + totalPrice* discount) ;
} else {
discount= 0.9 ;
System . out. println ( "折扣后总价为:" + totalPrice* discount) ;
}
}
}
4.3判断奇偶数
package com. la ;
import java. util. Scanner ;
public class Wrok1 {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入一个正整数(退出请输0)" ) ;
while ( true ) {
Integer i = scanner. nextInt ( ) ;
if ( i% 2 == 0 && i!= 0 && i> 0 ) {
System . out. println ( "该数" + i+ "为偶数" ) ;
} else if ( i== 0 ) {
break ;
} else {
System . out. println ( "该数" + i+ "为奇数" ) ;
}
}
}
}
4.4模拟中奖
package com. la ;
import java. util. Scanner ;
public class Work2 {
public static void main ( String [ ] args) {
int v = ( int ) ( Math . random ( ) * 10000 + 1 ) ;
Scanner scanne = new Scanner ( System . in) ;
System . out. println ( "请输入你的中奖号码" ) ;
while ( true ) {
int a= scanne. nextInt ( ) ;
if ( a== v) {
System . out. println ( "恭喜你中奖了" ) ;
} else {
System . out. println ( "再接再厉!" ) ;
}
System . out. println ( "奖励号码公布" + v) ;
}
}
}