1. 运算符
- 算术运算符:+、-、*、/、%、++、–
- 赋值运算符:=
- 关系运算符:>、<、>=、<=、==、!=、instanceof(布尔值)
- 逻辑运算符:&&(与)、||(或)、!
- 位运算符:&、|、^、~、>>、<<、>>>
- 条件运算符:?:
- 扩展赋值运算符:+=、-=、*=、/=
public class Day3_01 {
public static void main(String[] args) {
//ctrl+D:复制当前行到下一行
int a = 10;
int b = 20;
int c = 30;
int d = 40;
System.out.println(a+b); //30
System.out.println(a-b); //-10
System.out.println(a*b); //200
System.out.println(a/b); //0
System.out.println(a/(double)b); //0.5
long l = 1212121212111121L;
int i = 123;
short s = 10;
char ch = 3;
System.out.println(l+i+s+ch); //Long
System.out.println(i+s+ch); //Int
System.out.println(s+ch); //Int(没有long时,所有非int类型转为int)
}
}
public class Day3_02 {
public static void main(String[] args) {
int a = 3;
int b = a++;
int c = ++a;
System.out.println(a); //5
System.out.println(b); //3(a++保持a=3,然后再+1,a=4)
System.out.println(c); //5
//幂运算:很多运算都要借助工具来操作
double pow = Math.pow(2,3);
System.out.println(pow); //8.0
}
}
//逻辑运算符
public class Day3_03 {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println(a&&b); //false
System.out.println(a||b); //true
System.out.println(!(a&&b)); //true
//短路运算
int c = 5;
boolean d = (c<4)&&(c++>4);
System.out.println(d); //false
System.out.println(c);
//位运算
/*
A = 0011 1100
B = 0000 1101
---------------
A&B = 0000 1100(相同取相同,不同取0)
A|B = 0011 1101(相同取相同,不同取1)
A^B = 0011 0001(相同取0,不同取1)
~B = 1111 0010
2*8=16
<<
>>
0000 0000 0
0000 0001 1
0000 0010 2
0000 0100 4
0000 1000 8
*/
int x = 10;
int y = 20;
x+=y;
x-=y;
//字符串连接
System.out.println(x); //10
System.out.println(""+x+y); //1020
System.out.println(x+y+""); //30
//q ? w : e 若q为true则结果为w,否则为e
int score = 80;
String type = score < 60 ? "不及格" : "及格";
System.out.println(type);
}
}
2. Java流程控制
2.1 用户交互Scanner
实现程序和人的交互。通过Scanner类来获取用户的输入。
Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串。在读取前,需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
import java.util.Scanner;
public class Day3_scanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据:");
// if(scanner.hasNext()){
// String str = scanner.next();
// System.out.println("输入的内容为:" + str);
// }
if(scanner.hasNextLine()){
String s = scanner.nextLine();
System.out.println("输入的内容为:" + s);
}
scanner.close();
}
}
next():
- 一定要读取到有效字符后才可以结束输入
- 对输入有效字符之前遇到的空白,next()方法将会自动将其去掉
- 只有输入有效字符后才能将后面输入的空白作为分隔符或者结束符
- next()不能得到带有空格的字符串
nextLine():
- 以Enter为结束符,返回的是输入回车前的所有字符
- 可获得空白
import java.util.Scanner;
public class Day3_scanner2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = 0;
float f = 0.0f;
System.out.println("请输入整数:");
if(scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:"+i);
}else{
System.out.println("输入的不是整数数据!!");
}
System.out.println("请输入小数:");
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:"+ f);
}else{
System.out.println("输入的不是小数数据!");
}
}
}
import java.util.Scanner;
public class Day3_scanner3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
System.out.println("开始输入数值:");
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m++;
sum += x;
System.out.println("你输入了第" + m + "个数据,当前和为:" + sum);
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均值为" + (sum/m));
scanner.close();
}
}
2.2 结构
2.2.1 顺序结构
Java的基本结构就是顺序结构(最简单的算法结构),除非特别指明,否则就按照顺序一句一句执行。
2.2.2 选择结构:
-
if单选择结构
import java.util.Scanner; public class Day3_if01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); if(s.equals("Hello")){ System.out.println(s); } System.out.println("end"); scanner.close(); } }
-
if双选择结构
import java.util.Scanner; public class Day3_if02 { 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("恭喜你!"); }else{ System.out.println("继续加油!"); } scanner.close(); } }
-
if多选择结构
public class Day3_if03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入成绩:"); int score = scanner.nextInt(); if(score == 100){ System.out.println("满分"); }else if(score < 100 && score > 59){ System.out.println("及格"); }else if(score < 60 && score >= 0){ System.out.println("不及格"); }else{ System.out.println("输入错误"); } scanner.close(); } }
-
嵌套的if结构
public class Day3_if04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入成绩:"); int score = scanner.nextInt(); if(score >= 0 && score <= 100){ if(score > 59){ System.out.println("及格"); }else{ System.out.println("不及格"); } }else{ System.out.println("输入错误"); } scanner.close(); } }
-
switch多选择结构(switch case)
switch语句中的变量类型可以是:
- byte、short、int、char、String(JDK7开始)
- case标签必须为字符串常量或字面量
import java.util.Scanner;
public class Day3_switch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char grade = 'B';
switch (grade){
case 'A':
System.out.println("优秀");
break; //case 穿透
case 'B':
System.out.println("合格");
case 'C':
System.out.println("不合格");
default:
System.out.println("未知");
}
scanner.close();
}
}
输出:
合格
不合格
未知
注意:
case穿透现象,需要加上break!
2.2.3 循环结构
2.2.3.1 while循环
public class Day3_while {
public static void main(String[] args) {
//输出1-100
int i = 0;
while(i<100){
i++;
System.out.println(i);
}
}
}
public class Day3_while2 {
public static void main(String[] args) {
//输出1 + 2 + ... + 100
int i = 1;
int sum = 0;
while(i<=100){
sum += i;
i++;
}
System.out.println(sum);
}
}
- 只要布尔表达式为true,循环就会一直执行下去
- 需要一个让表达式失效的方式来结束循环
- 只有少部分需要循环一直执行,例如:服务器的请求响应监听
- 循环条件一直为true会造成无限循环(死循环),会影响程序性能或程序卡死
2.2.3.2 do…while循环
对于while语句而言,如果不满足条件,则不能进入循环。但有时候非得让它执行一次,就得用到do…while了。
public class Day3_doWhile {
public static void main(String[] args) {
int i = 1;
int sum = 0;
do{
sum = sum + i;
i++;
}while(i <= 100);
System.out.println(sum);
}
}
while和do…while的区别:
- while选判断后执行;do…while先执行后判断
- do…while保证循环体至少被执行一次
public class Day3_doWhile2 {
public static void main(String[] args) {
int a = 1;
while(a<1){
System.out.println(a);
a++;
}
System.out.println("==============");
do{
System.out.println(a);
a++;
}while(a<1);
}
}
输出:
==============
1
2.2.3.3 for循环(最重要!!!)
for(初始化;布尔表达式;更新){
//代码语句
}
1、计算0到100之间的奇数和偶数的和
public class Day3_for {
public static void main(String[] args) {
int sum1 = 0;
int sum2 = 0;
for(int i = 0; i <= 100; i++){
if(i % 2 == 0){
sum2 += i;
}else{
sum1 += i;
}
}
System.out.println(sum1);
System.out.println(sum2);
}
}
输出:
2500
2550
2、用while或for循环1-1000之间能被5整除的数,且每行输出3个
public class Day3_for2 {
//用while或for循环1-1000之间能被5整除的数,且每行输出3个
public static void main(String[] args) {
for(int i = 0; i <= 1000; i++){
if(i % 5 ==0){
System.out.print(i + "\t");
}
if(i % (5*3) == 0){
System.out.println();
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
3、打印九九乘法表

public class Day3_for3 {
//打印九九乘法表
public static void main(String[] args) {
/*
1. 先写列
2. 用一个循环将列包起来
3. 去重 i<=j
4. 调整输出格式,注意print和println
*/
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) { //9.for+回车
System.out.print(i + "*" + j + "=" + i*j +"\t");
}
System.out.println();
}
}
}