从方法内部调用显示方法

时间:2017-11-15 22:58:12

标签: java

我正在尝试编写一个程序,接受用户的两个数字。

这是我的计划的基本理念:

  

创建三种方法。一种方法将添加数字,一种方法将乘以数字,最后一种方法将显示数字。

我试图从前两个方法中调用display方法。 但该程序没有显示最终值。

   import java.util.InputMismatchException;
import java.util.Scanner;

public class ArithmeticMethods {
    public static void main(String [] args) {
        int num1, num2;
        int sum, mul;

        Scanner input = new Scanner(System.in);

        try{
            System.out.println("Enter the first number: ");
        num1 = input.nextInt();
        System.out.println("Enter the second number: ");
        num2 = input.nextInt();
        add( num1, num2);
        multiply(num1, num2);
            }
        catch (InputMismatchException e) {
                System.out.println("Invalid input, please try again.");
                System.out.println("note: text format is an invalid input.");
        }               
        finally{
            input.close();
        }   
}
    public static void add( int num1, int num2) {
        int sum = num1 + num2;
        display(sum,0);
    }
    public static void multiply( int num1, int num2) {
        int mul = num1 * num2;
        display(0, mul);
    }
    public static void display(int sum, int mul) {
        System.out.println("The result after adding the two numbers above is "+ sum +".");  
        System.out.println("The result after multiplying the two numbers above is "+ mul +".");

    }
}


output: Enter the first number: 
5
Enter the second number: 
2
The result after adding the two numbers above is 7.
The result after multiplying the two numbers above is 0.//Is there anyway I can
The result after adding the two numbers above is 0.//get rid of these two lines
The result after multiplying the two numbers above is 10.

2 个答案:

答案 0 :(得分:0)

您永远不会调用您实施的方法。在

中读取值后,请尝试以下操作
System.out.println("Enter the first number: ");
num1 = input.nextInt();
System.out.println("Enter the second number: ");
num2 = input.nextInt();
//call the your methods using the values read from the user
add(num1, num2);
multiply(num1, num2);

答案 1 :(得分:0)

您忘了从主电话中调用加法和乘法。

public class ArithmeticMethods {
    public static void main(String [] args) {
        int num1 = 0, num2 = 0;
        double sum, mul;

        Scanner input = new Scanner(System.in);

        try{
            System.out.println("Enter the first number: ");
        num1 = input.nextInt();
        System.out.println("Enter the second number: ");
        num2 = input.nextInt();
        add(num1,num2);
        multiply(num1,num2);

            }
        catch (InputMismatchException e) {
                System.out.println("Invalid input, please try again.");
                System.out.println("note: text format is an invalid input.");
        }               
        finally{
            input.close();
        }   

}
    public static void add( int num1, int num2) {
        int sum = num1 + num2;
        display(sum, sum);
    }
    public static void multiply( int num1, int num2) {
        int mul = num1 * num2;
        display(mul,mul);
    }
    public static void display(int sum, int mul) {
        System.out.println("The result after adding the two numbers above is "+ sum +".");
        System.out.println("The result after multiplying the two numbers above is "+ mul +".");
    }
}