所学知识点与代码
运算符
赋值运算符
public class OperatorDemo {
public static void main ( String[ ] args) {
int i = 10 ;
System. out. println ( "i:" + i) ;
i += 20 ;
System. out. println ( "i:" + i) ;
short s = 10 ;
s += 20 ;
System. out. println ( "s:" + s) ;
}
}
关系运算符
public class OperatorDemo {
public static void main ( String[ ] args) {
int i = 10 ;
int j = 20 ;
int k = 10 ;
System. out. println ( i == j) ;
System. out. println ( i == k) ;
System. out. println ( "--------" ) ;
System. out. println ( i != j) ;
System. out. println ( i != k) ;
System. out. println ( "--------" ) ;
System. out. println ( i > j) ;
System. out. println ( i > k) ;
System. out. println ( "--------" ) ;
System. out. println ( i >= j) ;
System. out. println ( i >= k) ;
System. out. println ( "--------" ) ;
System. out. println ( i = j) ;
}
}
逻辑运算符
public class OperatorDemo01 {
public static void main ( String[ ] args) {
int i = 10 ;
int j = 20 ;
int k = 30 ;
System. out. println ( ( i > j) & ( i > k) ) ;
System. out. println ( ( i < j) & ( i > k) ) ;
System. out. println ( ( i > j) & ( i < k) ) ;
System. out. println ( ( i < j) & ( i < k) ) ;
System. out. println ( "--------" ) ;
System. out. println ( ( i > j) | ( i > k) ) ;
System. out. println ( ( i < j) | ( i > k) ) ;
System. out. println ( ( i > j) | ( i < k) ) ;
System. out. println ( ( i < j) | ( i < k) ) ;
System. out. println ( "--------" ) ;
System. out. println ( ( i > j) ^ ( i > k) ) ;
System. out. println ( ( i < j) ^ ( i > k) ) ;
System. out. println ( ( i > j) ^ ( i < k) ) ;
System. out. println ( ( i < j) ^ ( i < k) ) ;
System. out. println ( "--------" ) ;
System. out. println ( ( i > j) ) ;
System. out. println ( ! ( i > j) ) ;
System. out. println ( ! ! ( i > j) ) ;
System. out. println ( ! ! ! ( i > j) ) ;
}
}
public class OperatorDemo02 {
public static void main ( String[ ] args) {
int i = 10 ;
int j = 20 ;
int k = 30 ;
System. out. println ( ( i > j) && ( i > k) ) ;
System. out. println ( ( i < j) && ( i > k) ) ;
System. out. println ( ( i > j) && ( i < k) ) ;
System. out. println ( ( i < j) && ( i < k) ) ;
System. out. println ( "--------" ) ;
System. out. println ( ( i > j) || ( i > k) ) ;
System. out. println ( ( i < j) || ( i > k) ) ;
System. out. println ( ( i > j) || ( i < k) ) ;
System. out. println ( ( i < j) || ( i < k) ) ;
System. out. println ( "--------" ) ;
System. out. println ( ( i++ > 100 ) && ( j++ > 100 ) ) ;
System. out. println ( "i:" + i) ;
System. out. println ( "j:" + j) ;
}
}
三元运算符
public class OperatorDemo {
public static void main ( String[ ] args) {
int a = 10 ;
int b = 20 ;
int max = a > b ? a : b;
System. out. println ( "max:" + max) ;
}
}
public class OperatorTest01 {
public static void main ( String[ ] args) {
int weight1 = 180 ;
int weight2 = 200 ;
boolean b = weight1 == weight2 ? true : false ;
System. out. println ( "b:" + b) ;
}
}
public class OperatorTest02 {
public static void main ( String[ ] args) {
int height1 = 150 ;
int height2 = 210 ;
int height3 = 165 ;
int tempHeight = height1 > height2 ? height1 : height2;
int maxHeight = tempHeight > height3 ? tempHeight : height3;
System. out. println ( "maxHeight:" + maxHeight) ;
}
}
算术运算符
public class OperatorDemo01 {
public static void main ( String[ ] args) {
int a = 6 ;
int b = 4 ;
System. out. println ( a + b) ;
System. out. println ( a - b) ;
System. out. println ( a * b) ;
System. out. println ( a / b) ;
System. out. println ( a % b) ;
System. out. println ( 6.0 / 4 ) ;
}
}
public class OperatorDemo02 {
public static void main ( String[ ] args) {
int i = 10 ;
char c = 'A' ;
c = 'a' ;
c = '0' ;
System. out. println ( i + c) ;
int j = i + c;
System. out. println ( j) ;
double d = 10 + 13.14 ;
}
}
public class OperatorDemo03 {
public static void main ( String[ ] args) {
System. out. println ( "it" + "heima" ) ;
System. out. println ( "itheima" + 666 ) ;
System. out. println ( 666 + "itheima" ) ;
System. out. println ( "黑马" + 6 + 66 ) ;
System. out. println ( 1 + 99 + "年黑马" ) ;
}
}
自增自减运算符
public class OperatorDemo {
public static void main ( String[ ] args) {
int i = 10 ;
System. out. println ( "i:" + i) ;
int j = ++ i;
System. out. println ( "i:" + i) ;
System. out. println ( "j:" + j) ;
}
}
数据输入
import java. util. Scanner;
public class ScannerDemo {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
int x = sc. nextInt ( ) ;
System. out. println ( "x:" + x) ;
}
}
import java. util. Scanner;
public class ScannerTest {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入第一个和尚的身高:" ) ;
int height1 = sc. nextInt ( ) ;
System. out. println ( "请输入第二个和尚的身高:" ) ;
int height2 = sc. nextInt ( ) ;
System. out. println ( "请输入第三个和尚的身高:" ) ;
int height3 = sc. nextInt ( ) ;
int tempHeight = height1 > height2 ? height1 : height2;
int maxHeight = tempHeight > height3 ? tempHeight : height3;
System. out. println ( "这三个和尚中身高最高的是:" + maxHeight + "cm" ) ;
}
}
分支语句
if语句
public class IfDemo {
public static void main ( String[ ] args) {
System. out. println ( "开始" ) ;
int a = 10 ;
int b = 20 ;
if ( a == b) {
System. out. println ( "a等于b" ) ;
}
int c = 10 ;
if ( a == c) {
System. out. println ( "a等于c" ) ;
}
System. out. println ( "结束" ) ;
}
}
public class IfDemo02 {
public static void main ( String[ ] args) {
System. out. println ( "开始" ) ;
int a = 10 ;
int b = 20 ;
b = 5 ;
if ( a > b) {
System. out. println ( "a的值大于b" ) ;
} else {
System. out. println ( "a的值不大于b" ) ;
}
System. out. println ( "结束" ) ;
}
}
import java. util. Scanner;
public class IfDemo03 {
public static void main ( String[ ] args) {
System. out. println ( "开始" ) ;
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入一个星期数(1-7):" ) ;
int week = sc. nextInt ( ) ;
if ( week == 1 ) {
System. out. println ( "星期一" ) ;
} else if ( week == 2 ) {
System. out. println ( "星期二" ) ;
} else if ( week == 3 ) {
System. out. println ( "星期三" ) ;
} else if ( week == 4 ) {
System. out. println ( "星期四" ) ;
} else if ( week == 5 ) {
System. out. println ( "星期五" ) ;
} else if ( week == 6 ) {
System. out. println ( "星期六" ) ;
} else {
System. out. println ( "星期日" ) ;
}
System. out. println ( "结束" ) ;
}
}
import java. util. Scanner;
public class IfTest01 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入一个整数:" ) ;
int number = sc. nextInt ( ) ;
if ( number% 2 == 0 ) {
System. out. println ( number + "是偶数" ) ;
} else {
System. out. println ( number + "是奇数" ) ;
}
}
}
import java. util. Scanner;
public class IfTest02 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入一个分数:" ) ;
int score = sc. nextInt ( ) ;
if ( score> 100 || score< 0 ) {
System. out. println ( "你输入的分数有误" ) ;
} else if ( score>= 95 && score<= 100 ) {
System. out. println ( "山地自行车一辆" ) ;
} else if ( score>= 90 && score<= 94 ) {
System. out. println ( "游乐场玩一次" ) ;
} else if ( score>= 80 && score<= 89 ) {
System. out. println ( "变形金刚玩具一个" ) ;
} else {
System. out. println ( "胖揍一顿" ) ;
}
}
}
顺序结构
public class OrderDemo {
public static void main ( String[ ] args) {
System. out. println ( "开始" ) ;
System. out. println ( "语句A" ) ;
System. out. println ( "语句B" ) ;
System. out. println ( "语句C" ) ;
System. out. println ( "结束" ) ;
}
}