繁体   English   中英

JAVA 递归距离伪代码帮助

[英]JAVA Recursive Distance Pseudocode Help

这是我为 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);
            }
    }


}

有谁知道我做错了什么?? 或者为什么我会崩溃

这是错误>>

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)
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 语句之外声明变量距离。 数组永远不会等于 integer,我假设您正在追求它的大小。

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

totalDistance ,我认为

if (x == 1)

可能应该改为

if (i == 1)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM