Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion 3.Java基本语法-运算符/10.BitOperationExercise.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class BitOperationExercise{
class BitOperationExercise {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
Expand All @@ -24,5 +24,14 @@ public static void main(String[] args) {
System.out.println(num1);
System.out.println(num2);

//第2题
int i1 = 60;
int i2 = i1 & 15;
String j = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + "";
int temp = i1 >>> 4;
i2 = temp & 15;
String k = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + "";
System.out.println(k + "" + j);

}
}
23 changes: 22 additions & 1 deletion 3.Java基本语法-运算符/10.位运算练习.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,25 @@ class BitOperationExercise{

}
}
```
```

## 10.2 实现60的二进制到16进制的转换

利用无符号位右移实现: >>>

```java
class BitOperationExercise {
public static void main(String[] args) {

//第2题
int i1 = 60;
int i2 = i1 & 15;
String j = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + "";
int temp = i1 >>> 4;
i2 = temp & 15;
String k = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + "";
System.out.println(k + "" + j);

}
}
```
24 changes: 24 additions & 0 deletions 3.Java基本语法-运算符/13.OperationExercise.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class OperationExercise {
public static void main(String[] args) {
//第3题
int x = 10, y = 20, z = 30;
int max = ((x > y ? x : y) > z) ? (x > y ? x : y) : z;
System.out.println("x y z中最大值是: " + max);

//第4题
double m = 11.0, n = 12.0;
if (m > 10.0 && n < 20.0) {
System.out.println("m+n: " + (m + n));
} else {
System.out.println("m: " + x + ",n: " + y);
}

//第5题
int i = 10;
int j = 20;
i = i ^ j;
j = i ^ j;
i = i ^ j;
System.out.println("i: " + i + ",j: " + j);
}
}
65 changes: 65 additions & 0 deletions 3.Java基本语法-运算符/13.复习题.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 13. 复习题

1.& 和 && 的异同

2.程序输出

```java
class IfElse{
public static void main(String[] args) {
boolean x = true;
boolean y = false;
short z = 40;
if ((z++==40) && (y=true)){
z++;
}
if((x=false) || ++z==43){
z++;
}
System.out.println("z = " + z);
}
}
```

3.定义三个int型变量并赋值,使用三元运算符或者if-else获取这三个数中的较大数的实现。

```java
class OperationExercise {
public static void main(String[] args) {
//第3题
int x = 10, y = 20, z = 30;
int max = ((x > y ? x : y) > z) ? (x > y ? x : y) : z;
System.out.println("x y z中最大值是: " + max);
}
}
```

4.编写程序:声明2个double型变量并赋值。判断第一个数大于10.0,且第2个数小于200,打印两数之和,否则,打印两数的乘积。
```java
class OperationExercise {
public static void main(String[] args) {
//第4题
double m = 11.0, n = 12.0;
if (m > 10.0 && n < 20.0) {
System.out.println("m+n: " + (m + n));
} else {
System.out.println("m: " + x + ",n: " + y);
}
}
}
```

5.交换两个变量值的代码实现
```java
class OperationExercise {
public static void main(String[] args) {
//第5题
int i = 10;
int j = 20;
i = i ^ j;
j = i ^ j;
i = i ^ j;
System.out.println("i: " + i + ",j: " + j);
}
}
```
4 changes: 3 additions & 1 deletion 3.Java基本语法-运算符/5.比较运算符.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@


## 5.1 比较运算符注意
* 比较运算“==”不能写成“=”。
* 比较运算“==”不能写成“=”。
* \>、 <、 \>=、 <= 只能用在数值型数据之间。
* ==、!= 不仅可以用在数值型数据之间,还可以使用在其他引用类型变量之间。
3 changes: 3 additions & 0 deletions 3.Java基本语法-运算符/9.位运算符.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

位运算符是直接对整数的二进制进行的运算。

`注意`: 没有<<<。


(下表中\代表|)

| 运算符 | 运算 | 范例 |
Expand Down
Binary file modified 3.Java基本语法-运算符/BitOperationExercise.class
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions 4.Java基本语法-流程控制/1.流程控制.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 1. 流程控制

## 1.1 流程控制概念
流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑块。

## 1.2 流程控制分类

流程控制采用结构化程序设计中规定的三种基本流程结构:
* 顺序结构
* 分支结构
* 循环结构

### 顺序结构
程序从上到下逐行地执行,中间没有任何判断和跳转。

### 分支结构
根据条件,选择性地执行某段代码。
* if-else
* switch-case

### 循环结构
根据循环条件,重复性地执行某段代码。
* while
* do...while
* for

`注意`: JDK1.5提供了foreach循环,方便遍历集合、数组元素。

42 changes: 42 additions & 0 deletions 4.Java基本语法-流程控制/2.IfElse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class IfElse{
public static void main(String[] args) {
//举例1
int heartBeats = 78;
if (heartBeats < 60 || heartBeats > 100) {
System.out.println("需要做进一步检查");
}
System.out.println("检查结束");

//举例2
int age = 23;
if (age < 18) {
System.out.println("还可以多看动画片");
} else {
System.out.println("可以开始工作了");
}

//举例3
if (age < 0) {
System.out.println("输入数据非法");
} else if (age < 18) {
System.out.println("青少年时期");
} else if (age < 35) {
System.out.println("青壮年时期");
} else if (age < 60) {
System.out.println("中年时期");
} else {
System.out.println("老年时期");
}

boolean x = true;
boolean y = false;
short z = 40;
if ((z++==40) && (y=true)){
z++;
}
if((x=false) || ++z==43){
z++;
}
System.out.println("z = " + z);
}
}
57 changes: 57 additions & 0 deletions 4.Java基本语法-流程控制/2.if-else.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 2. if-else

## 2.1 if-else格式
条件判断结构。


### 格式1: 单独if
```
if(条件表达式){
执行代码块;
}
```

![if-else格式1](../4.Java基本语法-流程控制-assets/ifelse-1.jpg)


### 格式2: if-else
```
if(条件表达式){
执行代码块1;
}
else{
执行代码块2;
}
```
![if-else格式2](../4.Java基本语法-流程控制-assets/ifelse-2.jpg)


### 格式3: if-else if-else

```
if(条件表达式1){
执行代码块1;
}
else if(条件表达式2){
执行代码块2;
}
...
else{
执行代码n;
}
```

![if-else格式2](../4.Java基本语法-流程控制-assets/ifelse-3.jpg)


## 2.2 if-else注意事项
* else结构是可选的
* 针对条件表达式:
* 如果多个条件表达式之间是"互斥"关系(没有交集关系),哪个判断和执行语句声明在上面还是下面无所谓。
* 如果多个条件表达式之间有"交集"关系,需要根据实际情况,考虑清楚应该将哪个结构声明在上面。
* 如果多个条件表达式之间有"包含"关系,通常情况下,需要将范围小的声明在范围大的上面,否则,范围小的就没机会执行。
* if-else结构是可以相互嵌套的,但是最多不要超过3层
* 如果if-else结构的执行语句只有1行时,对应的一对{}是可以省略的,但是不建议省略
* 如果存在多个if但是只有一个else,else会采用就近原则,和else最近的一个if进行配对
* 获取随机数:
> 公式: [a, b]: (int)(Math.random()*(b-a+1) + a)
Loading