简体   繁体   中英

Java print the recursion in main method

I was wondering when I tried to print the value of recursion in main, the answer was:

Enter the number: 1

2The result is:

How to make the number 2 to the front like,

The result is: 2

import java.util.Scanner;

public class Question4Final {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int a = scan.nextInt();

        System.out.printf("The result is: ", multiplication(a));
    }

    public static int multiplication(int a) {

        if (a == 5) {
            int multiply = 10 * 6 * 2;
            System.out.print(multiply);
        } else if (a == 4) {
            int multiply2 = 6 * 2;
            System.out.print(multiply2);
        } else if (a == 1) {
            System.out.print("2");
        }
        return a;
    }
}

To call the method:

System.out.printf("The result is: ", multiplication(a));

first the arguments must be evaluated, so multiplication(a) is executed before System.out.printf("The result is: ", multiplication(a)) . Since multiplication(a) prints something, that printing takes place before "The result is:" is printed.

You should change multiplication(a) to simply return the result without printing it. Then use System.out.println("The result is: " + multiplication(a)) to print the result.

Note the you have to change the value returned by multiplication(a) , since currently you return a , which is not the value printed by that method.

You have 2 issues in your code.

  1. First is you are printing the value of 'multiply' in your static method:

    public static int multiplication(int a){

    System.out.print(multiply);

That is a reason why it is printing 2 before the statement:

2The result is:
  1. 2nd issue is you are calling the method multiplication in the print statement:

    System.out.printf("The result is: ", multiplication(a));

That is not how to print the result by calling the method. I have taken your example and run the below code. You can check this code.

import java.util.Scanner;

public class Main
{
     public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int a = scan.nextInt();
        int product = multiplication(a);

        System.out.println("The result is : " +product);
    }

    public static int multiplication(int a){

        int multiply = 0;

        if(a == 5){
            multiply = 10 * 6 * 2;
        }else if(a == 4){
            multiply = 6 * 2;
        }else if(a == 1){
            multiply = 2;
        }
        return multiply; 
    }
}

Below are the outputs on different options:

Enter the number: 4                                                                                                                                 
The result is : 12

Enter the number: 5                                                                                                                                 
The result is : 120 

Enter the number: 1                                                                                                                                 
The result is : 2 

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