一、回顾
变量:内存中空间用于存储数据
数据类型:
8种基本数据类型:二进制补码
byte short/char int long
float double
boolean:true/false
byte: -128~127
char: 0~65535 '\u0000' ~ '\uFFFF'
A:65 a:97 '1':48
引用类型
除了基本数据类型,都是引用
数组、类、接口、枚举...
运算符:
算数运算符 + - * / % ++ --
逻辑运算符 &&/& ||/| 短路
boolean f = a++ < 1 && b++ < 2; a=3,b=4
a = 4, b = 4
位运算符 & | ^ !
比较运算符 > < = >= <= == !=
三目运算符 boolean ? value1:value2
位移运算 >> << >>>[无符号右移]
命名规则:
变量:都是小写,多个单词拼接,首字母小写,第二个开始单词首字母大写
helloWorld -> 驼峰命名法
类名:首字母大写,驼峰命名法
HelloWorld
包名:都是小写,域名倒写 www.baidu.com
com.baidu.baike.xx
二、逻辑控制语句
顺序结构、条件分支
if(判断的boolean类型的表达式) {}
if - else :可以互相嵌套,但是嵌套次数不宜太多,<=3次最多
if - else if - ... - else 多种情况时使用
if 不能省,其他的else都可以省略
在if中判断相等的情况下:
可以用switch-case替代
switch中可以放置的变量类型有:
int\short\byte\char
JDK1.7以后 支持String
枚举
Demo 01 if 条件分支——判断一个年份是不是闰年
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
System.out.println("请输入年份: ");
Scanner console = new Scanner(System.in);
int year = console.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "年,是闰年");
}else {
System.out.println(year + "年,不是闰年");
}
}
}
Demo 02 if 条件分支——编写一个收银柜台收款程序
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入单价(¥):");
double price = console.nextDouble();
System.out.println("请输入数量:");
int count = console.nextInt();
System.out.println("请输入金额(¥):");
double money = console.nextDouble();
double sum = price * count;
if (sum >= 500) {
sum *= 0.8;
}
if (money >= sum) {
System.out.println("应付金额:" + sum + ",找零:" + (money-sum));
} else {
System.out.println("金额有误!");
}
}
}
Demo 03 if 条件分支——给分数评级 0~100
public class Demo03 {
public static void main(String[] args) {
int score = 100;
if (score >= 90) {
System.out.println("分数A+");
} else {
if (score >= 85) {
System.out.println("分数A");
} else {
if (score >= 80) {
System.out.println("分数B");
} else {
if (score >= 70) {
System.out.println("分数C");
} else {
if (score >= 60) {
System.out.println("分数D");
} else {
System.out.println("分数E");
}
}
}
}
}
}
}
Demo 04 if - else if - … - else ——给分数评级 0~100
public class Demo05 {
public static void main(String[] args) {
int score = 100;
if (score >= 90) {
System.out.println("分数A+");
} else if (score >= 85) {
System.out.println("分数A");
} else if (score >= 80) {
System.out.println("分数B");
} else if (score >= 70) {
System.out.println("分数C");
} else if (score >= 60) {
System.out.println("分数D");
} else {
System.out.println("分数E");
}
}
}
Demo 05 if - else if - … - else ——控制台输入月份, 判断月份中有几天 1~12,并输出
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入月份(1~12):");
int month = console.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
System.out.println(month + "月中有31天。");
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println(month + "月中有30天。");
} else if (month == 2) {
System.out.println(month + "月中有28天。");
} else {
System.out.println("月份有误!");
}
}
}
Demo 06 switch-case ——控制台输入月份, 判断月份中有几天 1~12,并输出
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入月份(1~12):");
int month = console.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month + "月中有31天。");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month + "月中有30天。");
break;
case 2:
System.out.println(month + "月中有28天。");
break;
default:
System.out.println("月份有误!");
}
}
}
循环结构
重复执行的事情,有规律的事情
for - 有规律的
while - 当...
1.先判断有没有答对
2.再循环猜数字
do-while - 做...直到...
1.第一次不用判断,直接猜
2.猜完,才要判断循环要不要继续
结论:while和do-while在一定程度上可以通用
只有在第一次就不符合循环条件时,有区别
do-while至少执行1次,while可能会执行0次
break: 结束当前循环,可以借助变量名来结束外层循环
continue: 结束本次循环,继续下一次循环
Demo 01——for 循环初试
public class Demo08 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++){
System.out.println(i);
}
int i = 0;
for (; i < 10; i++){
System.out.println(i);
}
for (int j = 0; j < 10;){
System.out.println(j);
j++;
}
for (int j = 0; ; j++){
if (j >= 10) {
break;
}
System.out.println(j);
}
for (;;) {
}
}
}
Demo 02——for 循环求 1~50 的和
public class Demo09 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 50; i++) {
sum += i;
}
System.out.println(sum);
}
}
Demo 03——while 循环输出
public class Demo10 {
public static void main(String[] args) {
int j = 0;
while (j < 10) {
j++;
if (j==5){
continue;
}
System.out.println(j);
}
}
}
Demo 04——for 循环中break的使用
public class Demo11 {
public static void main(String[] args) {
loop1: for (int i = 0; i < 2; i++){
int j = 0;
loop2: while (j < 3) {
j++;
System.out.println(j);
if (j == 2) {
break loop1;
}
}
}
}
}
Demo 05——for 循环实现100以内加法
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int score = 0;
for (int i = 1; i <= 10; i++) {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
System.out.println("(" +i +")."+a + " + " + b + " = ?");
System.out.print("请输入答案,输入-1退出:");
int answer = console.nextInt();
if (answer == -1) {
break;
}
if (answer == (a + b)) {
System.out.println("答对了!");
score += 10;
} else {
System.out.println("答错了!");
}
}
System.out.println("最终分数为:" + score);
}
}
Demo 06——while 循环猜字母游戏
import java.util.Scanner;
public class Demo13 {
public static void main(String[] args) {
int random = (int) (Math.random() * 1000 + 1);
System.out.println("最终结果是:" + random);
Scanner console = new Scanner(System.in);
int a = -1;
while (a != random) {
System.out.println("请输入你猜的数字,输入0退出:");
a = console.nextInt();
if (a == 0) {
break;
}
if (a > random) {
System.out.println("太大了");
} else if (a < random) {
System.out.println("太小了");
}
}
if (a == random) {
System.out.println("恭喜你答对了!");
} else {
System.out.println("很遗憾!没有答对!");
}
}
}
Demo 06——do – while 实现猜字母游戏
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
int random = (int) (Math.random() * 1000 + 1);
System.out.println("最终结果是:" + random);
Scanner console = new Scanner(System.in);
int a;
do {
System.out.println("请输入你猜的数字,输入0退出:");
a = console.nextInt();
if (a == 0) {
break;
}
if (a > random) {
System.out.println("太大了");
} else if (a < random) {
System.out.println("太小了");
}
} while (a != random);
if (a == random) {
System.out.println("恭喜你答对了!");
} else {
System.out.println("很遗憾!没有答对!");
}
}
}
Demo 07——循环嵌套,命名
public class Demo08Loop {
public static void main(String[] args) {
int a = 0;
x:for(int i = 0; i < 5; i++) {
y:for(int j = 0; j < 3; j++) {
if (j == 1) {
continue;
}
System.out.println("i = " + i + ", j = " + j);
}
}
for(int i = 1 ;i <= 9; i++) {
for (int j = 1; j <= i; j++) {
}
}
}
}