目录
一、顺序结构
程序执行的顺序和代码执行的顺序有关
public class LogicalLearning{
public static void main(String[] args){
System.out.println("aa");
System.out.println("bb");
}
}
二、分支结构
1.if语句
(1)基本语法形式1
if(布尔表达式){
//条件满足时执行代码
}
(2)基本语法形式2
if(布尔表达式){
//条件满足时执行代码
}else{
//条件不满足时执行代码
}
(3)基本语法形式3
if(布尔表达式){
//条件满足时执行代码
}else if(布尔表达式){
//条件满足时执行代码
}else{
//条件都不满足时执行代码
}
注意:
(1)悬垂 else 问题:else 是和最接近的 if 匹配
(2)if / else 语句中可以不加 大括号 . 但是也可以写语句(只能写一条语句)。实际开发中我们 不建议 这么写. 最好加上大括号
(3)代码风格问题:
//风格1:
if(布尔表达式){
//条件满足时执行代码
}else{
//条件不满足时执行代码
}
//风格2:
if(布尔表达式)
{
//条件满足时执行代码
}
else
{
//条件不满足时执行代码
}
public class LogicalLearning{
public static void main(String[] args){
int x = 20;
if (x == 10); {
System.out.println("hehe");
}
}
}
编译并运行该代码,输出如下:
hehe
此处多写了一个 分号, 导致分号成为了 if 语句的语句体, 而 { } 中的代码已经成为了和一个 if 无关的代码块
2.switch语句:
(1)基本语法:
switch(整数|枚举|字符|字符串){
case 内容1 : {
内容满足时执行语句;
[break;]
}
case 内容2 : {
内容满足时执行语句;
[break;]
}
...
default:{
内容都不满足时执行语句;
[break;]
}
}
switch(num >= 10 && num <= 20){
...
}
这种情况,我们建议用if语句
if(num >= 10 && num <= 20){
...
}
(4)switch 虽然支持嵌套, 但是很丑,如:
public class LogicalLearning{
public static void main(String[] args){
int x = 1;
int y = 1;
switch(x) {
case 1:
switch(y) {
case 1:
System.out.println("hehe");
break;
}
break;
case 2:
System.out.println("haha");
break;
}
}
}
三、循环结构
1.while循环
(1)基本语法格式
while(循环条件){
循环语句;
}
循环条件必须为布尔表达式,循环条件为 true, 则执行循环语句; 否则结束循环
注意:
(1)while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
(2)while 后面的 { 建议和 while 写在同一行
(3)while 后面不要多写 分号, 否则可能导致循环不能正确执行
public class LogicalLearning{
public static void main(String[] args){
int num = 1;
while (num <= 10); {
System.out.println(num);
num++;
}
}
}
2.for循环
(1)基本语法形式:
for(表达式1;表达式2;表达式3){
循环体;
}
3.do-while循环
do{
循环语句;
}while(循环条件);
先执行循环语句, 再判定循环条件,所以循环体至少被执行一次 。do while 循环最后的分号不要忘记
(2)一般 do while 很少用到, 更推荐使用 for 和 while.
注意: Java里面没有goto语句,goto作为一个保留字,随时会被启用
四、输入输出
1.输出到控制台
转换符 | 类型 | 举例 | 输出 |
d |
十进制整数
|
("%d", 100)
| 100 |
x |
十六进制整数
|
("%x", 100)
| 64 |
o |
八进制整数
|
("%o", 100)
| 144 |
f |
定点浮点数
|
("%f", 100f)
| 100.000000 |
e |
指数浮点数
|
("%e", 100f)
| 1.000000e+02 |
g |
通用浮点数
|
("%g", 100f)
| 100.000 |
a |
十六进制浮点数
|
("%a", 100)
| 0x1.9p6 |
s |
字符串
|
("%s", 100)
| 100 |
c |
字符
| ("%c",'1') | 1 |
b |
布尔值
| ("%b", 100) | true |
h |
散列码
| ("%h", 100) | 100 |
% |
百分号
| ("%.2f%%",2/7f) | 0.29% |
这个表格只要在用的时候查看就可。
2.从键盘输入
import java.util.Scanner; // 需要导入 util 包
public class LogicalLearning{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.nextLine(); //nextLine()是一个方法 从控制台输入一个字符串到你的变量name
//String name = sc.next();//next()是一个方法 这样也可以从控制台输入一个字符串到你的变量name,但是这样写遇空格结束
System.out.println("请输入你的年龄:");
int age = sc.nextInt(); //nextInt()是一个方法 从控制台输入一个int到你的变量age
//注意:一定要先输入字符串再输入整数,否则整数输入不了
System.out.println("请输入你的工资:");
float salary = sc.nextFloat();
System.out.println("你的信息如下:");
System.out.println("姓名: "+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary);
sc.close(); // 注意, 要记得调用关闭方法
}
}
编译并运行该代码,输出如下:
请输入你的姓名:笑笑请输入你的年龄:30请输入你的工资:1000你的信息如下:姓名 : 笑笑年龄:30工资: 1000.0
(2)使用 Scanner 循环读取 N 个数字 :
import java.util.Scanner
public class LogicalLearning{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int n = scanner.nextInt();
System.out.println(n);
}
}
}