开发者

JAVA Recursive Distance Pseudocode Help

开发者 https://www.devze.com 2023-03-13 12:42 出处:网络
This is the Pseudocode I have written for a JAVA application to calculate distance using coordinates.

This is the Pseudocode I have written for a JAVA application to calculate distance using coordinates.

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)

I am not really great with Java, but I wrote the below out. When I pass variables to it it crashes.

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(开发者_如何学JAVAx, 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);
            }
    }


}

Anyone know what I am doing wrong?? Or why I am getting a crash

This is ERROR>>

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);
        }
}

You need to declare the return type to double instead of void. You meed to declare the variable distance outside of the if statement. An array will never equal an integer, I assume you're going after its size.

There are still some mistakes with your logic here, but I don't want to do all of your homework for you.


In totalDistance, I think

if (x == 1)

probably should be changed to

if (i == 1)

Then, if i != 1, you would skip the calculation of distance, but try to use it anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消