简体   繁体   中英

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. 3. Add a line in the main that calls the fToC method and passes as its argument the variable declared in step 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 :( )

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(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; , but your method is expecting a float . If you pass double to a method expecting a float , then it will not compile. You need to change double variable into float variable .

NOTE NOTE : One more thing. You should name your variables appropriately. Even calling it value is better than calling it variable . The name variable tells the reader nothing about the purpose of it.

NOTE x 3 : Another thing I noticed, is you're setting your variable to a value of 0 , yet the question specifies:

In the main method declare a variable of type double and initialize it to a value. 3.

So you should think about replacing that 0 with something glaringly obvious.

to perform:

In the main method declare a variable of type double and initialize it to a value. 3. Add a line in the main that calls the fToC method and passes as its argument the variable declared in step 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). Here is how you need to modify the first part of the main method:

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. Then your main () function does nothing but included the embedded ftoC () function. Nothing actually ever gets called.

The solution is to remove the inner 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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