3.17
快捷键
psvm—框架
sout—输出
Main.java
1 2 3 4 5
| public class Main { public static void main(String[] args) { System.out.println("Hello and welcome!"); } }
|
整型

强制类型转换
1 2 3
| int a = 4660; byte b; b=(byte)a;
|

0x 十六进制(0-9和A-F组成)
1 2
| 0x55=85 byte b=0x55;//b=85
|
0.7E-3 科学计数法
变量
栈–票;堆–储物柜
常量定义
1 2
| //final 关键字声明常量 final double PI=3.14D;//变量名字母全部大写,单词间用下划线分隔,值不能改变
|

运算符(七种)
算术运算符

% 取模
++ 自增
1 2 3 4
| //前置++:先运算再取值 int a=5; int b; b=++a;//++a==>a=a+1;a=6 b=a;b=6 a=6
|
1 2 3
| //后置++:先取值再运算 b=a++;//a=5,b=a,a=a+1 //b=5;a=6
|
– 自减
运算示例
1 2 3 4 5 6 7
| //规律:取模运算结果与被除数一致 7/-5=-1 -7/5=-1 -7%5=-2 7%-5=2 -7.0/5=-1.4 7/-5.0=-1.4 7%5.0=2.0 -7%5.0=-2.0 //被除数小于除数,取模的结果是被除数本身 5%7=5
|
关系运算符
3.24
布尔逻辑运算符(六种,返回结果为布尔类型)
&(与)、|(或)、^(异或,不同为真,相同为假)、!(非)、&&(and)、||(or)
x>3 && x<6 //3<x<6
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Scanner sc = new Scanner(System.in); System.out.println("输入整数:"); int num = sc.nextInt();
if(num!=0 && 100/num>0) {
System.out.println("执行了if语句块"); } else{
System.out.println("条件不成立,执行else语句块") }
|

位运算符(只用于整数)
&、|、^、~、<<、>>、>>>(无符号右移,强制将结果变为正数)
左移公式:m<<n=m*(2^n)
右移公式:m>>n=m/(2^n)
1 2 3 4
| int b = -8;
System.out.println("无符号右移:"+(b>>>1));
|

赋值类运算符
=、+=、-=、*=、%=、&=、|=、^=、<<=、>>=(中间不能加空格)
条件运算符(三目运算符)
1 2 3
| A?B:C ①A =》true =》B ②A =》false =》C
|
练习

1 2 3 4 5 6 7 8 9 10 11
| public class Main { public static void main(String[] args) { int fh = 177; int mh = 165; double sh = (fh + mh) * 1.08 / 2; double dh = (fh * 0.923 + mh) / 2; System.out.println("儿子身高: " + String.format("%.2f", sh) + "CM"); System.out.println("女儿身高: " + String.format("%.2f", dh) + "CM\n"); } }
|
1 2
| 儿子身高: 184.68CM 女儿身高: 164.19CM
|
1 2 3 4 5 6 7 8 9 10
| public class Main { public static void main(String[] args) { int hm = 21 * 2 + 3; int lm = 24 * 2; System.out.println("小红现在有: " + hm + "元"); System.out.println("小蓝现在有: " + lm + "元"); System.out.println("两人钱数是否相同: " + (hm == lm) + "\n"); } }
|
1 2 3
| 小红现在有: 45元 小蓝现在有: 48元 两人钱数是否相同: false
|
1 2 3 4 5 6 7 8 9 10
| public class Main { public static void main(String[] args) { double money1 = 24+8+3; money1 = (money1>30)?money1*0.8:money1; double money2 = 16+8+3; double money = money1<money2?money1:money2; System.out.println(money); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class Main { public static void main(String[] args) { int m=1,n=2,o=3,p=4; System.out.println("ok="+((m+n)*o/(p-o)+p)); System.out.println("ok="+(m+n)*o/(p-o)+p); System.out.println("ok="+p%n); System.out.println("ok="+(m>n)); System.out.println("ok="+(p>=p)); System.out.println("ok="+(p!=p)); System.out.println("ok="+((p>m)&&(p<m))); System.out.println("ok="+((p>m)||(p<m))); boolean OK = (p!=p)&&(n<o); System.out.println(OK); boolean Ok = (p!=p)||(n<o); System.out.println(Ok); System.out.println(p*ok); System.out.println("p="+p*ok); System.out.println("p="+p*ok+1); System.out.println(p*ok+1); String jj=p>m?"yes":"no"; System.out.println(jj); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ok=13 ok=94 ok=0 ok=false ok=true ok=false ok=false ok=true false true
yes
|
3.31
语句
if-else语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.println("请输入成绩:"); int score = sc.nextInt(); if (score >= 0) { System.out.println("及格了!"); } else { System.out.println("不及格!"); } System.out.println("输入的是专业课成绩"); } }
|
闰年判断:能被4整除但不能被100整除,或者能被400整除的年份是闰年。
eg:2000年是闰年,1900年不是闰年,2020年是闰年,2021年不是闰年。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.println("请输入年份:"); int year = sc.nextInt(); if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year+"是闰年!"); } else{ System.out.println(year+"不是闰年!"); } } }
|
多重if-else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.println("请输入成绩:"); int score = sc.nextInt(); if (score >= 90) { System.out.println("A"); }
else if (score >= 80 && score < 90) { System.out.println("B"); } else if (score >= 70 && score < 80) { System.out.println("C"); } else if (score >= 60 && score < 70) { System.out.println("D"); } else { System.out.println("E"); } } }
|
while语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Main { public static void main(String[] args) {
int num = 0; int sum = 0; while (num < 10) { num++; sum += num; System.out.println(num+"\t"); } System.out.println(sum); } }
|
do-while语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Main { public static void main(String[] args) {
int num = 0; int sum = 0; do{ num++; sum += num; System.out.println(num+"\t"); }while(num<10); System.out.println(sum); } }
|
while与do-while区别
while:先验条件,可能一次都不执行
do-while:至少执行一次,后验条件
for语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Main { public static void main(String[] args) {
int num = 0; int sum = 0; for (int i = 1; i <= 100; i++) { System.out.println(i+"\t"); sum += i; } System.out.println("求和操作:"+sum); } }
|
双重for循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Main { public static void main(String[] args) {
int num = 0; int sum = 0; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + j * i + " "); } System.out.println(); } } }
|
4.7
switch语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class Main { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); System.out.println("请输入年份(yyyy):"); int year = scanner.nextInt(); System.out.println("请输入月份(MM):"); int month = scanner.nextInt(); int day = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("大月:31天"); break; case 2: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { System.out.println("2月:29天"); } else{ System.out.println("2月:28天"); } break; case 4: case 6: case 9: case 11: System.out.println("小月:30天"); break; default: System.out.println("输入的月份不对!"); } } }
|
break语句(终止循环)
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Main { public static void main(String[] args) {
for(int x=1; x<=10; x++) { if(x==5){ break; } System.out.println(x+"\t"); } } }
|
continue语句(跳出本次循环,继续进行下一次循环)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Main { public static void main(String[] args) {
for(int x=1; x<=10; x++) { if(x==5){ continue; } System.out.println(x+"\t"); } } }
|
4.14
第三章 类和对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.dhjk.classes;
public class Student { private String name; private String sex; private int age; private void say(){ System.out.println("每天按时上课"); } public static void main(String[] args){ Student student = new Student(); student.name ="张三"; student.sex ="男"; student.age =18; System.out.println(student.toString());
Student student2 = new Student(); student2.name ="李四"; student2.sex ="女"; student2.age =18; System.out.println(student2.toString()); }
@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } }
|