You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.7 KiB
Markdown

3 years ago
总的来说Java的条件判断if与选择语法与C一致。这一章我们通过几个例子来说明Java的上述语法同时对一些Java的特性进行讲解。
## 1. 布尔类型与布尔计算
Java的布尔类型和C保持一致基本的运算符也和C保持一致。
**注意,布尔类型是 boolean并有大写的布尔类型Boolean。这两个类型是不同的小写的是基本类型与C一致大写的表示封装类型是面向对象的类型。**
### 1.1. 关系运算
![](img/20230303170551.png)
### 1.2. 逻辑运算
![](img/20230303170617.png)
![](img/20230303170628.png)
![](img/20230303170639.png)
![](img/20230303170647.png)
![](img/20230303170657.png)
### 1.3. 位运算
### 1.4. 选择语句
### 1.5. 运算优先级
尽量使用括号
### 1.6. 补充例子
#### 1.6.1. 如何循环的读取输入
这个方式在书上没有讲,在考试中可能会遇到这种情况。
3 years ago
```java {.line-numbers}
3 years ago
import java.util.Scanner;
/**
* 循环读取,一直到某个特定的条件退出
*
* @author Danny
*
*/
public class LoopRead {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input a sequence of integer, divided by space.");
int sum = 0;
while (true) {
if (scanner.hasNextInt()) {
sum += scanner.nextInt();
} else {
break;
}
}
scanner.close();
System.out.println("The sum is: " + sum);
}
}
```
## 2. 重点
本章的内容中的大部分技术要点都与C保持一致因此之需要掌握几个例子就可以了
1. AdditionQuiz
2. SubtractionQuiz
3. ComputeAndInterpretBMI
4. ComputeTax
5. ChineseZodiac
6. 本页的补充例子,循环读取输入