Java 代码中 for 循环中的错误计数 i++ - Prim 算法
Wrong count i++ in for loop in Java code - Prim's algorithm
我有一个大问题。我是 Java 的初学者。请帮助我,谢谢。
这部分代码是Prim算法。
密码是:
for (int i = 1; i < graph.getNumOfNodes(); i++) {
double distance = ((int)graph.getEdges(i) - 8);
if (graph.getEdges(i) <= 8){
System.out.println("Day " + i + ". " + "Number of hours worked " + (int)graph.getEdges(i) + "/8 per day. Finished kilometers: " + (int)graph.getEdges(i) );
} else if (graph.getEdges(i) > 8) {
System.out.println("Day " + i + ". " + "Number of hours worked 8/8 per day. Finished kilometers: 8. " + "Missing to complete: " + (int)distance);
System.out.println("Day " + i + ". " + "Number of hours worked " + (int)distance + "/8 per day. Finished kilometers: " + (int)distance);
}
}
现在输出:第 3 天是两倍 但我需要第 2 天第 3 天是第 4 天,其余的会更大。
Day 1. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 2. Number of hours worked 7/8 per day. Finished kilometers: 7
Day 3. Number of hours worked 8/8 per day. Finished kilometers: 8. Missing to complete: 2
Day 3. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 4. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 5. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 6. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 7. Number of hours worked 1/8 per day. Finished kilometers: 1
Day 8. Number of hours worked 8/8 per day. Finished kilometers: 8
Day 9. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 10. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 11. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 12. Number of hours worked 8/8 per day. Finished kilometers: 8
我想要这样的输出:
Day 1. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 2. Number of hours worked 7/8 per day. Finished kilometers: 7
Day 3. Number of hours worked 8/8 per day. Finished kilometers: 8. Missing to complete: 2
Day 4. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 5. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 6. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 7. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 8. Number of hours worked 1/8 per day. Finished kilometers: 1
Day 9. Number of hours worked 8/8 per day. Finished kilometers: 8
Day 10. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 11. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 12. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 13. Number of hours worked 8/8 per day. Finished kilometers: 8
我只想添加一个计数器来计算额外的天数,如下所示:
extraDays = 0
for (int i = 1; i < graph.getNumOfNodes(); i++) {
double distance = ((int)graph.getEdges(i) - 8);
if (graph.getEdges(i) <= 8) {
System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked " + (int)graph.getEdges(i) + "/8 per day. Finished kilometers: " + (int)graph.getEdges(i) );
} else if (graph.getEdges(i) > 8) {
System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked 8/8 per day. Finished kilometers: 8. " + "Missing to complete: " + (int)distance);
extraDays += 1
System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked " + (int)distance + "/8 per day. Finished kilometers: " + (int)distance);
}
}
我有一个大问题。我是 Java 的初学者。请帮助我,谢谢。 这部分代码是Prim算法。
密码是:
for (int i = 1; i < graph.getNumOfNodes(); i++) {
double distance = ((int)graph.getEdges(i) - 8);
if (graph.getEdges(i) <= 8){
System.out.println("Day " + i + ". " + "Number of hours worked " + (int)graph.getEdges(i) + "/8 per day. Finished kilometers: " + (int)graph.getEdges(i) );
} else if (graph.getEdges(i) > 8) {
System.out.println("Day " + i + ". " + "Number of hours worked 8/8 per day. Finished kilometers: 8. " + "Missing to complete: " + (int)distance);
System.out.println("Day " + i + ". " + "Number of hours worked " + (int)distance + "/8 per day. Finished kilometers: " + (int)distance);
}
}
现在输出:第 3 天是两倍 但我需要第 2 天第 3 天是第 4 天,其余的会更大。
Day 1. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 2. Number of hours worked 7/8 per day. Finished kilometers: 7
Day 3. Number of hours worked 8/8 per day. Finished kilometers: 8. Missing to complete: 2
Day 3. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 4. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 5. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 6. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 7. Number of hours worked 1/8 per day. Finished kilometers: 1
Day 8. Number of hours worked 8/8 per day. Finished kilometers: 8
Day 9. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 10. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 11. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 12. Number of hours worked 8/8 per day. Finished kilometers: 8
我想要这样的输出:
Day 1. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 2. Number of hours worked 7/8 per day. Finished kilometers: 7
Day 3. Number of hours worked 8/8 per day. Finished kilometers: 8. Missing to complete: 2
Day 4. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 5. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 6. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 7. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 8. Number of hours worked 1/8 per day. Finished kilometers: 1
Day 9. Number of hours worked 8/8 per day. Finished kilometers: 8
Day 10. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 11. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 12. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 13. Number of hours worked 8/8 per day. Finished kilometers: 8
我只想添加一个计数器来计算额外的天数,如下所示:
extraDays = 0
for (int i = 1; i < graph.getNumOfNodes(); i++) {
double distance = ((int)graph.getEdges(i) - 8);
if (graph.getEdges(i) <= 8) {
System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked " + (int)graph.getEdges(i) + "/8 per day. Finished kilometers: " + (int)graph.getEdges(i) );
} else if (graph.getEdges(i) > 8) {
System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked 8/8 per day. Finished kilometers: 8. " + "Missing to complete: " + (int)distance);
extraDays += 1
System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked " + (int)distance + "/8 per day. Finished kilometers: " + (int)distance);
}
}