swap.md 617 字节
Newer Older
M
Mars Liu 已提交
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
# 变量和赋值

假设有a b两个整形变量,交换他们的值,可以用下列代码中的:

## aop

### before

```java
int a = 10;
int b = 20;
System.out.printf("before swap: a=%d, b=%d\n", a, b);
```

### after

```java
System.out.printf("after swap: a=%d, b=%d\n", a, b);
```

## 答案

```java

int tmp = a;
a = b;
b = tmp;

```

## 选项

### 常量使用错误

```java
const int tmp = a;
a = b;
b = tmp;
```

### 类型错误

```java
auto tmp = a;
a = b;
b = tmp;
```

### Java 没有指针

```java
int* tmp = &a;
a = b;
b = tmp;
```

### 变量未定义

```java
tmp = a;
a = b;
b = tmp;
```