子程序和方法

时间:2019-10-17 21:14:31

标签: java methods

试图按照评论中的说明进行操作,但是我已经待了好几个小时,并认为我需要经验丰富的人的帮助。基本上我希望从用户那里获得a和b的值,这些值将被传递到一段代码中,该代码将计算出幅度,然后将其传递到一段显示幅度的代码中,代码如下所示:

import java.util.Scanner;

/**
 * Software Development 1, Lab 3 Task 1: Write a program to calculate the
 * magnitude of a 2D vector The magnitude calculation should take place in a
 * separate method.
 */
public class OneVector {

    public static void main(String[] args) {
        readComponents();
        double magnitude = calculateMagnitude(a);
        displayMag(magnitude);
    }

    // TODO: Read in the two components from the user
    public static double readComponents() {
        Scanner scan = new Scanner(System.in);
        System.out.println("enter a value for vector 1");
        double a = scan.nextDouble();
        System.out.println("enter a value for vector 2");
        double b = scan.nextDouble();
        return (a);
        return (b);

    }

    // TODO: Use the calculateMagnitude method to find its magnitude
    static void calculateMag() {

    }

    // TODO: Display the vector's magnitude to the user
    public static void displayMag(double magnitude) {
        System.out.println(magnitude);

    }

    /*
     * Calculates the magnitude of a 2D vector.
     */
    public static double calculateMagnitude(double a, double b) {
        // TODO: Write code to calculate the vector's magnitude
        double magnitude = Math.sqrt((a * a) + (b * b));

        // TODO: Return the calculated magnitude to the main method
        return magnitude;
    }
}

2 个答案:

答案 0 :(得分:0)

以下是正确的答案:

import java.util.Scanner;

/**
 * Software Development 1, Lab 3 Task 1: Write a program to calculate the
 * magnitude of a 2D vector The magnitude calculation should take place in a
 * separate method.
 */
public class OneVector {
    static double a,b;
    public static void main(String[] args) {
        readComponents();
        double magnitude = calculateMagnitude(a,b);
        displayMag(magnitude);
    }

    // TODO: Read in the two components from the user
    public static void readComponents() {
        Scanner scan = new Scanner(System.in);
        System.out.println("enter a value for vector 1");
        a = scan.nextDouble();
        System.out.println("enter a value for vector 2");
        b = scan.nextDouble();
    }

    // TODO: Use the calculateMagnitude method to find its magnitude
    static void calculateMag() {

    }

    // TODO: Display the vector's magnitude to the user
    public static void displayMag(double magnitude) {
        System.out.println(magnitude);

    }

    /*
     * Calculates the magnitude of a 2D vector.
     */
    public static double calculateMagnitude(double a, double b) {
        // TODO: Write code to calculate the vector's magnitude
        double magnitude = Math.sqrt((a * a) + (b * b));

        // TODO: Return the calculated magnitude to the main method
        return magnitude;
    }
}

答案 1 :(得分:0)

 readComponents()
中的

send return语句将永远不会执行,有关基本概念,您可以参考这本书:

Head First Java