一、运算符
- 算术运算符:+(加)、-(减),*(乘),/(除),%(取余)
-
- 整数相除只能得到整数
-
- 字符加操作:算术表达式中包含多个基本数据类型的值的时候,整个表达式的类型会自动提升
-
-
- byte类型,short类型和char类型将被提升为int类型
- 整个表达式的类型,自动提升到表达式中的最高等级操作数同样的类型。
- byte,short,char<int<long<float<double
-
-
- 字符串加操作:直接拼接(从左至右依次运算)
- 赋值运算法:=,+=,-=,*=,/=,%=
-
- 扩展的赋值运算符底层隐含了强制类型转换;
- 自增自减运算符:++,--
-
- i++ 与 i-- 的区别
- 关系运算符:==,!=,>,>=,<,<=
-
- 结果一定为true , false
- 逻辑运算符:&,|,^,!
- 短路逻辑运算符:&&,||
-
- &&:左边为假,右边则不执行
- ||:左边为真,右边不执行
- 三元运算符:
-
- 格式: 关系表达式 ?表达式1 :表达式2;
- 范例:a > b ? a : b
- 若为TRUE,表达式1 就是结果;反之就是表达式2
二、数据输入
Scanner的使用基本步骤
//导包 //导包的动作必须出现在类定义的上边。 import java.util.Scanner; //创建对象 //只有SC是变量名可以改变,其他的均不可变。 Scanner sc = new Scanner(System.in); //接受数据 //只有I是变量名可以改变,其他的均不变。 int i = sc.nextInt(); //范例 import java.util.Scanner; import class ScannerDemo { Scanner sc = new Scanner(System.in) int x = sc.nextInt(); System.out.println("x:"+ x ); }
三、顺序结构
- if语句
//格式一 public class IfDemo { public statics void mian(System[] args) { int a = 10; int b = 20; if (a == b) { System.out.println("a等于b"); } System.out.println("a不等于b"); } //格式二 public calss IfDemo { public statics void main(System[] args) { int a = 10; int b = 20; if(a > b) { System.out.println("a大于b"); }else{ System.out.println("a不大于b"); } } //格式三 import java.util.Scanner; public class IfDemo { public static void main(string[] args) { System.out.println("开始"); Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); int week = sc.newInt(); if (week == 1) { System.out.println("星期一"); }else if(week == 2) { System.out.println("星期二"); }else if(week == 3) { System.out.println("星期三"); }else { System.out.println("other "); }
四、Switch语句
import java.util.Scanner; public class SwichDemo { public static void main(string[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个星期数:"); int week = sc.nextInt(); switch(week) { case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; default: System.out.println("有误") } }
五、for循环语句
public class ForDemo { public static void main(string[] args) { for(int i=1; i<5; i++){ System.out.println("helloword"); } } }
六、while循环结构
public class WhileDemo { public static void main(string[] args) { int j = 1; while(j<=5) { System.out.println("helloword"); j++; } } }
七、do...while结构
public class DoDemo { public xtatic void main(string[] args) { int j = 1; do { System.out.println("helloword"); j++; }while(j<=5); } }
八、跳转控制语句
continue与break
public class ConttolDemo { public static void main(string[] args) { for(int i=1;i<=5;i++) { //跳出,开始下一次循环 continue; //break;跳出后结束循环 } System.out.println(i); } }
本文含有隐藏内容,请 开通VIP 后查看