简体   繁体   English

如何在Java中的main方法之外调用方法

[英]how to call on a method outside of the main method in java

I have a method outside of the main method that averages. 我的平均方法以外的方法。 At the end of the main I am calling on that averaging method. 在主体结尾处,我要调用该平均方法。 I am not understaning the propper way to 1. use variables I declare with other methods in this program. 我不是在理解1.使用在此程序中与其他方法一起声明的变量的正确方法。 for example the sum which is defined in the calculate_average method. 例如,在calculate_average方法中定义的总和。 or The sum which is defined in the main method. 或main方法中定义的总和。

Please show me 请给我看

  1. how to interchange variables in and out of the main method. 如何在主要方法之间来回交换变量。 interchangeably between methods that are not in main. 在非主要方法之间可以互换。

  2. call on a method outside of main in main to calculate the average. 调用main之外的方法来计算平均值。

Here is my current code. 这是我当前的代码。

import java.util.ArrayList;
import java.util.Scanner;

class programTwo
{

    private static Double calculate_average( ArrayList<Double> myArr )
    {
        Double Sum = 0.0;
        for (Double number: myArr)
        {
            Sum += number;
        }

    }

    public static void main( String[] args )
    {
        Scanner scan = new Scanner(System.in);
        ArrayList<Double> myArr = new ArrayList<Double>();
        double Sum = 0;
        int count = 0;
        System.out.println("Enter a number to be averaged, repeat up to 20 times:");
        String inputs = scan.nextLine();

    while (!inputs.matches("[qQ]") )
    {

        if (count == 20)
        {
            System.out.println("You entered more than 20 numbers, you suck!");
            break;
        }

        Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input
        myArr.add(scan2.nextDouble());
        count += 1;
        System.out.println("Please enter another number or press Q for your average");

        inputs = scan.nextLine();
    }
    Double average = calculate_average(myArr);
    System.out.println("Your average is: " + average);

    return Sum / myArr.size();

}}

You have this at the end of main : main的结尾处有这个:

return Sum / myArr.size();

You need to move it to calculate_average : 您需要将其移动到calculate_average

private static Double calculate_average( ArrayList<Double> myArr )
{
    Double Sum = 0.0;
    for (Double number: myArr)
    {
        Sum += number;
    }

    return Sum / myArr.size();
}

Given that main is void and an average makes no sense as an exit code even if it weren't, I'm assuming that is some kind of typographical error. 鉴于mainvoid并且即使不是,平均数也没有退出代码的意义,我假设这是某种印刷错误。

Some code review 一些代码审查

  • Local variables should start with a lower case letter. 局部变量应以小写字母开头。 So sum , not Sum 所以sum ,而不是Sum
  • You can use the primitive double instead of a Double object. 您可以使用原始double而不是Double对象。 Using the object will cause boxing/unboxing overhead. 使用该对象将导致装箱/拆箱的开销。
  • Indent your while loop and its contents by four more spaces so the loop beginning aligns vertically with the code above it because it's in the same block (the method's block). 使while循环及其内容缩进四个空格,使循环开始处与其上方的代码垂直对齐,因为该循环位于同一块(方法的块)中​​。
  • Split those last 2 braces into separate lines 将最后两个大括号拆分为单独的行
  • Line up the last two braces vertically with the matching opening brace 将最后两个大括号与匹配的大括号垂直对齐
  • You've started by creating your calculate_average method, but continue breaking up main . 您首先创建了calculate_average方法,但继续拆分main It's too long. 它太长了。 Too much is going on there. 那里发生了太多事情。
  • Change if (count == 20) to if (count >= 20) . if (count == 20)更改为if (count >= 20) Harder to mess up, then. 那么,就很难搞砸了。

i think you have static method , so you can call them bye using className.MethodName Directly. 我认为您有静态方法 ,因此可以直接使用className.MethodName来调用它们。

Just Declare your method as public and you can call them outside also. 只需将您的方法声明为公共方法,也可以在外部调用它们。

 programTwo.calculate_average(myArr)

No need to create object for calling static methods. 无需创建用于调用静态方法的对象。

There's nothing I can see wrong with your code, except you are missing a return statement in your calculate_average method . 我看不到您的代码有什么问题,除了您在calculate_average方法中缺少return语句之外。

Also, if you name a method calculate_average , it should return the average not the sum . 另外,如果您命名方法calculate_average ,则它应返回平均值而不是sum

Try this: 尝试这个:

private static Double calculate_average( ArrayList<Double> myArr )
{
    Double sum = 0.0;
    for (Double number: myArr)
    {
        sum += number;
    }
    return sum/myArr.size(); // added return statement
}

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

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