簡體   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