英文:
Java program doesn't show an output
问题
我有以下问题,每次运行这个程序时,它都显示一个空屏幕。我不知道我漏掉了什么。
我试图解决的问题是给定两条道路(数组),其中每个元素表示通过所需的时间,找到最短的路径。你只能在两条道路之间切换一次。
public class MyClass {
public static int shortestRoad(int[] road1, int[] road2) {
return shortestRoadNumbers(road1, road2, 0);
}
private static int shortestRoadNumbers(int[] road1, int[] road2, int index) {
if (index == road1.length || index == road1.length) {
return 0;
}
if (road1[index] >= road2[index] && road1[index + 2] >= road2[index + 2]){
return (road2[index] + shortestRoadNumbers(road1, road2, index + 1));
}
else
return (road1[index] + shortestRoadNumbers(road1, road2, index + 1));
}
public static void main(String args[]) {
int[] road1 = new int[] { 5, 4, 5, 8, 12, 9, 9, 3 };
int[] road2 = new int[] { 7, 3, 3, 12, 10, 2, 10, 7 };
MyClass.shortestRoad(road1, road2);
}
}
英文:
I have the following problem, every time I run this program, it shows an empty screen. I don't know What I am missing.
The problem I am trying to solve is giving two roads (arrays) in which each element represents the time it takes to go through, find the shortest path. You can switch between two roads only one time.
public class MyClass {
public static int shortestRoad(int[] road1, int[] road2) {
return shortestRoadNumbers(road1, road2, 0);
}
private static int shortestRoadNumbers(int[] road1, int[] road2, int index) {
if (index == road1.length || index == road1.length) {
return 0;
}
if (road1[index] >= road2[index] && road1[index + 2] >= road2[index + 2]){
return (road2[index] + shortestRoadNumbers(road1, road2, index + 1));
}
else
return (road1[index] + shortestRoadNumbers(road1, road2, index + 1));
}
public static void main(String args[]) {
int[] road1 = new int[] { 5, 4, 5, 8, 12, 9, 9, 3 };
int[] road2 = new int[] { 7, 3, 3, 12, 10, 2, 10, 7 };
MyClass.shortestRoad(road1, road2);
}
}
</details>
# 答案1
**得分**: 0
看起来你正在调用`shortestRoad`函数,但没有保留也没有打印输出。
以下方法输出结果。
```java
int result = MyClass.shortestRoad(road1, road2);
System.out.println(result);
英文:
It appears that you're calling the shortestRoad
function, but not retaining nor printing the output.
The following approach outputs the result.
int result = MyClass.shortestRoad(road1, road2);
System.out.println(result);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论