简体   繁体   English

如何在方法中调用方法

[英]How to call a method within a method

While this may be incredible simple, I am having trouble with it. 尽管这可能非常简单,但是我遇到了麻烦。

I had to create a Fahrenheit to Celsius, and Celsius to Fahrenheit converter. 我必须创建一个摄氏到华氏度,以及摄氏到华氏度的转换器。 The assignment after this one is to: 此后的作业是:

In the main method declare a variable of type double and initialize it to a value. 在main方法中,声明一个double类型的变量并将其初始化为一个值。 3. Add a line in the main that calls the fToC method and passes as its argument the variable declared in step 2. 3.在主行中添加一行调用fToC方法,并将在步骤2中声明的变量作为参数传递。

I know that too declare a variable and set it to a number, I will have to do this: 我知道也要声明一个变量并将其设置为数字,我将必须这样做:

double var = 0;

But I do not know how to call in the method in this. 但是我不知道如何在这个方法中调用。

I tried calling the method, but it seems to be improper format, and I can not find how to properly do it (Googled quite a bit :( ) 我尝试调用该方法,但是它似乎格式不正确,而且我找不到正确的方法(Googled相当多:()

import java.util.Scanner;

public class ExerciseOne {
    public static void main( String[] args ) {

        int choice;
        double variable = 0;

        public static float ftoC(double var) {    //This line is giving me trouble

            System.out.println( "What would you like to do? \n \n 1 - Fahnrenheit to Celsius \n 2 - Celsius to Fahrenheit \n 3 - Quit" );
            Scanner dylan = new Scanner(System.in);
            choice = dylan.nextInt();

            if (choice == 1)
            {
                System.out.println( "What number do you want to convert to Celsius?" );
                float input = dylan.nextFloat();
                float output = ftoC(input);
                System.out.println( input + " degrees Fahrenheit is " +
                    ftoC(input) + " degrees Celsius." );
            }

            if (choice == 2)
            {
                System.out.println( "What number do you want to convert to Fahrenheit?" );
                float input = dylan.nextFloat();
                float output = ctoF(input);
                System.out.println( input + " degrees Celsius is " +
                    ctoF(input) + " degrees Fahrenheit." );
            }

            if (choice == 3)
            {
                System.out.println( "Exiting application.");
            }
        }
    }

    public static float ftoC(float f)
    {
        float celsius = (f-32)*(5f/9f);
        return celsius;
    }

    public static float ctoF(float c)
    {
        float fahrenheit = c*9/5 + 32;
        return fahrenheit;
    }
}

To call the ftoC method, you use the following syntax: 要调用ftoC方法,请使用以下语法:

ftoC(x); // Assuming x is the name of the float you created.

NOTE : One thing I noticed in your example, is you're declaring the value to pass in as double variable = 0; 注意 :我在您的示例中注意到的一件事是,您声明将值作为double variable = 0;传递double variable = 0; , but your method is expecting a float . ,但是您的方法要求float If you pass double to a method expecting a float , then it will not compile. 如果将double传递给需要float的方法,则它将无法编译。 You need to change double variable into float variable . 您需要将double variable更改为float variable

NOTE NOTE : One more thing. 注意注意 :还有一件事。 You should name your variables appropriately. 您应该适当地命名变量。 Even calling it value is better than calling it variable . 甚至称它为value比称其为variable要好。 The name variable tells the reader nothing about the purpose of it. 名称variable没有告诉读者任何目的。

NOTE x 3 : Another thing I noticed, is you're setting your variable to a value of 0 , yet the question specifies: 注意x 3 :我注意到的另一件事是,您将variable设置为0 ,但问题指定了:

In the main method declare a variable of type double and initialize it to a value. 在main方法中,声明一个double类型的变量并将其初始化为一个值。 3. 3。

So you should think about replacing that 0 with something glaringly obvious. 因此,您应该考虑将其0替换为显而易见的内容。

to perform: 去表演:

In the main method declare a variable of type double and initialize it to a value. 在main方法中,声明一个double类型的变量并将其初始化为一个值。 3. Add a line in the main that calls the fToC method and passes as its argument the variable declared in step 2. 3.在主行中添加一行调用fToC方法,并将在步骤2中声明的变量作为参数传递。

you just have to call the method and assign the result to the variable like you already did in the rest of the main method (for options 1 and 2). 您只需要调用该方法并将结果分配给变量即可,就像在主方法的其余部分中所做的一样(对于选项1和2)。 Here is how you need to modify the first part of the main method: 这是您需要修改main方法的第一部分的方法:

import java.util.Scanner;

public class ExerciseOne
{
    public static void main( String[] args ) 
    {

        int choice;
        float initialF = 3; // assign variable to 3 initially

        float resultC = ftoC(variable); // simply call the method and assign the result to a variable

…then the rest of the main method as you have it. …然后剩下的主要方法就可以了。

Your code is a little confused. 您的代码有些混乱。 You've defined ftoC() twice, once within main () and once as its own function. 您已经定义了ftoC()两次,一次在main ()内,一次作为其自己的函数。 Then your main () function does nothing but included the embedded ftoC () function. 然后,您的main ()函数除了包含嵌入式的ftoC ()函数外什么也不做。 Nothing actually ever gets called. 实际上什么都没有被调用。

The solution is to remove the inner ftoC (). 解决方案是删除内部的ftoC ()。

Note: you could have defined ftoC () and ctoF () within the body of main (), but there's nothing to be gained, really, and nobody does it that way. 注意:可能已经在main ()的主体中定义了ftoC ()和ctoF (),但实际上没有任何收获,而且没有人这样做。

import java.util.Scanner;

public class ExerciseOne {
    public static void main( String[] args ) {

        int choice;
        double variable = 0;

        System.out.println( "What would you like to do?\n\n" );
        System.out.println( " 1 - Fahnrenheit to Celsius\n" );
        System.out.println( " 2 - Celsius to Fahrenheit\n" );
        System.out.println( " 3 - Quit]n" );
        Scanner dylan = new Scanner(System.in);
        choice = dylan.nextInt();

        if (choice == 1)
        {
            System.out.println( "What number do you want to convert to Celsius?" );
            float input = dylan.nextFloat();
            float output = ftoC(input);
            System.out.println( input + " degrees Fahrenheit is " +
                ftoC(input) + " degrees Celsius." );

        }

        if (choice == 2)
        {
            System.out.println( "What number do you want to convert to Fahrenheit?" );
            float input = dylan.nextFloat();
            float output = ctoF(input);
            System.out.println( input + " degrees Celsius is " +
                ctoF(input) + " degrees Fahrenheit." );
        }

        if (choice == 3)
        {
            System.out.println( "Exiting application.");
        }
    }

    public static float ftoC(float f) { /* same as before */ }
    public static float ctoF(float f) { /* same as before */ }
}

There were other things that could be fixed, but they're beyond the scope of this question. 还有其他一些可以解决的问题,但这超出了此问题的范围。

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

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