Java 子程序和方法

Java 子程序和方法,java,methods,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 Th

我试着按照评论中的说明去做,但我已经做了好几个小时了,我想我需要一个更有经验的人的帮助。基本上,我希望从用户处接收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;
    }
}

以下是正确答案:

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

以下是正确答案:

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

readComponents()中的send return语句永远不会执行,有关基本概念,您可以参考以下书籍: readComponents()

Head-First Java

readComponents()中的send-return语句永远不会执行,有关基本概念,您可以参考一本书: readComponents()

Head-First Java

您尝试从
readComponents()
返回两个值,但在堆栈溢出方面存在一些问题,因此您可以查找它们。但是这个方法甚至需要返回两个值吗?如果只返回一个值并调用此方法两次,不是会更容易吗?您尝试从
readComponents()
返回两个值,并且存在关于堆栈溢出的问题,因此可以查找它们。但是这个方法甚至需要返回两个值吗?如果只返回一个值并调用此方法两次,不是会更容易吗