JAVA递归距离伪代码帮助

时间:2011-06-15 00:05:47

标签: java recursion

这是我为JAVA应用程序编写的伪代码,用于使用坐标计算距离。

totalDistance (int[] x, int[] y, int i)
    If x = 1 then
        distance =  pow(x[i] – x[i – 1], 2) + pow(y[i] – y[i – 1], 2)
        return  round(sqrt(distance))
    else
        return round(sqrt(distance)) + totalDistance(x, y, i – 1)

我对Java并不是很了解,但我写了下面的内容。当我将变量传递给它时,它会崩溃。

import java.util.Scanner;



class distance {

    public static void main(String[] args) {
        System.out.println("Welcome to Travel Bliss Distance Calculator!");
        Scanner input = new Scanner(System.in);
        int[] x = new int[5];
        int[] y = new int[5];
        String[] city = new String[5];

        int i=0;
        for (i=0; i < 5;){

            System.out.println("Enter X Coordinates>>");
            x[i] = input.nextInt();
            System.out.println("Enter Y Coordinates>>");
            y[i] = input.nextInt();
            System.out.println("With Coordinates: (" + x[i] + "," + y[i] + ") ");
            i++;
        }
        totalDistance(x, y, i);
    }


    public static double totalDistance(int[] x, int[] y, int i){
        double distance;
        if (i == 1){
               distance = pow(x[i] – x[i – 1], 2) + pow(y[i] – y[i – 1], 2);
               return distance;
            }
            else {
                return round(sqrt(distance)) + totalDistance(x,y,i-1);
            }
    }


}

任何人都知道我做错了什么?或者为什么我会崩溃

这是ERROR&gt;&gt;

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token "[", Expression expected after this token
    distance cannot be resolved to a variable
    The type of the expression must be an array type but it resolved to int
    Syntax error on token "Invalid Character", [ expected
    The type of the expression must be an array type but it resolved to int
    Syntax error on token "Invalid Character", [ expected
    Syntax error, insert "]" to complete ArrayAccess
    Syntax error, insert "]" to complete ArgumentList
    The type of the expression must be an array type but it resolved to int
    Syntax error on token "Invalid Character", [ expected
    The type of the expression must be an array type but it resolved to int
    Syntax error on token "Invalid Character", [ expected
    Syntax error, insert "]" to complete ArrayAccess
    Syntax error, insert "]" to complete ArgumentList
    distance cannot be resolved to a variable
    Syntax error on token "Invalid Character", invalid AssignmentOperator

    at distance.totalDistance(distance.java:30)
    at distance.main(distance.java:24)

2 个答案:

答案 0 :(得分:1)

public static double totalDistance(int[] x, int[] y, int i){
         int distance = pow(x[i] – x[i – 1], 2) + pow(y[i] – y[i – 1], 2);
        if (x.length == 1){
           return distance;
        }
        else {
            return round(sqrt(distance)) + totalDistance(x,y,i-1);
        }
}

您需要将返回类型声明为double而不是void。 你应该在if语句之外声明变量distance。 一个数组永远不会等于一个整数,我假设你正在追求它的大小。

这里的逻辑仍有一些错误,但我不想为你做完所有的功课。

答案 1 :(得分:1)

totalDistance,我认为

if (x == 1)

可能应该改为

if (i == 1)

然后,如果i != 1,您会跳过distance的计算,但无论如何都要尝试使用它。