int index = 1; for (int i = 1; i < 10; i++) { index = index++; } System.out.print(index); 结果为什么是1
时间: 2023-12-07 18:05:53 浏览: 29
该代码的输出结果为1。
原因是在循环中,index = index++ 这行代码实际上等价于两行代码:
```
int temp = index;
index = index + 1;
index = temp;
```
因为++运算符是后缀运算符,它会先将原始值赋值给临时变量temp,然后再将index加1,最后再将temp的值赋值给index。这就导致了代码中index的值始终没有被改变,因此循环结束后index的值仍然为1。
如果要让index的值在循环中递增,可以将index = index++ 改为 index++ 或者 ++index。例如:
```
int index = 1;
for (int i = 1; i < 10; i++) {
index++;
}
System.out.print(index);
```
这样输出的结果就是10了。
相关问题
#include<iostream> #include<cmath> #include<algorithm> using namespace std; struct point { int x, y, index; double theta; } p[30000], temp; int f(point& a, point& b) { if (a.theta == b.theta) { if (a.x == b.x && a.y > 0) return a.y * a.y > b.y * b.y; else return a.x * a.x + a.y * a.y < b.x* b.x + b.y * b.y; }return a.theta < b.theta; }int main() { int n; cin >> n; //input for (int i = 0; i < n; i++) { int x, y; cin >> p[i].x >> p[i].y; p[i].index = i + 1; }//exchange min_x to first position int min = p[0].x, minPosition = 0; for (int i = 1; i < n; i++) { minPosition = min <= p[i].x ? minPosition : i; min = min <= p[i].x ? min : p[i].x; }temp = p[0]; p[0] = p[minPosition]; p[minPosition] = temp; //cal atan2 for (int i = 1; i < n; i++) { p[i].x -= p[0].x; p[i].y -= p[0].y; p[i].theta = atan2(p[i].y, p[i].x); }//sort exclude min_x sort(p + 1, p + n, f); //output cout << n << endl; for (int i = 0; i < n; i++) { if (p[i].index == 1) { for (int j = i; j < n; j++) cout << p[j].index << endl; for (int j = 0; j < i; j++) cout << p[j].index << endl; break; } } }转为Java
以下是将给定的 C++ 代码转换为 Java 代码的结果:
```java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static class Point {
int x, y, index;
double theta;
public Point(int x, int y, int index, double theta) {
this.x = x;
this.y = y;
this.index = index;
this.theta = theta;
}
}
static Point[] p = new Point[30000];
static int f(Point a, Point b) {
if (a.theta == b.theta) {
if (a.x == b.x && a.y > 0)
return a.y * a.y > b.y * b.y ? 1 : -1;
else
return a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y ? 1 : -1;
}
return a.theta < b.theta ? 1 : -1;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
// input
for (int i = 0; i < n; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
p[i] = new Point(x, y, i + 1, 0);
}
// exchange min_x to first position
int min = p[0].x, minPosition = 0;
for (int i = 1; i < n; i++) {
minPosition = min <= p[i].x ? minPosition : i;
min = min <= p[i].x ? min : p[i].x;
}
Point temp = p[0];
p[0] = p[minPosition];
p[minPosition] = temp;
// cal atan2
for (int i = 1; i < n; i++) {
p[i].x -= p[0].x;
p[i].y -= p[0].y;
p[i].theta = Math.atan2(p[i].y, p[i].x);
}
// sort exclude min_x
Arrays.sort(p, 1, n, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
return f(a, b);
}
});
// output
System.out.println(n);
for (int i = 0; i < n; i++) {
if (p[i].index == 1) {
for (int j = i; j < n; j++)
System.out.println(p[j].index);
for (int j = 0; j < i; j++)
System.out.println(p[j].index);
break;
}
}
scan.close();
}
}
```
使用集合定义一个队列 class+Queue+{+ ++++List<Integer>+list+=+new+ArrayList<Integer>();+ ++++int+index+=+0;
使用Java集合定义一个队列可以使用LinkedList类,该类实现了Queue接口,因此可以直接使用。以下是一个使用LinkedList定义队列的例子:
```java
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.add(3);
queue.add(7);
queue.add(1);
queue.add(6);
queue.add(2);
System.out.println(queue); // 输出:[3, 7, 1, 6, 2]
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)