Java中的方法/函数

时间:2014-04-22 13:32:03

标签: java args

使用给定的代码我得到了指示:Java允许我们使用方法/函数,因此我们可以存储我们可能多次使用的过程,所以我希望您更新代码,在那里可以执行常见任务在方法/函数内完成。知道怎么做吗?

package fifthAssignment;

public class Arithmetic {

    public static void main(String[] args) {

        // setting up the variable firstNumber and secondNumber
        int length = args.length;

        if (length != 3) {
            System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
            return;
        }
        int firstNumber = Integer.parseInt(args[0]);
        int secondNumber = Integer.parseInt(args[1]);
        int addition = firstNumber + secondNumber;
        int minus = firstNumber - secondNumber;
        int division = firstNumber / secondNumber;
        int multiply = firstNumber * secondNumber;
        String arithmetic = args[2];

        if (arithmetic.equals("+")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + addition);


        } else if (arithmetic.equals("-")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + minus);


        } else if (arithmetic.equals("/")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + division);


            // I could not use "*" operator as it was looking to pull down all
            // the files associated with this program instead of
            // using it the way I intended to use it. So in this case I changed
            // the "*" to "x" so that I can get the solution you
            // were looking for.
        } else if (arithmetic.equals("x")) {

            System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + multiply);

        }
        // following prints out to the console what the length of each argument
        // is.
        System.out.println(args[0] + " has the length of " + args[0].length());
        System.out.println(args[1] + " has the length of " + args[1].length());

        if (arithmetic.equals("+")) {
            int total = String.valueOf(addition).length();
            System.out.println(addition + " has the length of " + total);
        }else if (arithmetic.equals("-")) {
            int total = String.valueOf(minus).length();
            System.out.println(minus + " has the length of " + total);
        }else if (arithmetic.equals("/")) {
            int total = String.valueOf(division).length();
            System.out.println(division + " has the length of " + total);
        } else if (arithmetic.equals("x")) {
            int total = String.valueOf(multiply).length();
            System.out.println(multiply + " has the length of " + total);
        }

    }
}

2 个答案:

答案 0 :(得分:2)

我将提供一个单独的例子,但你应该自己做。

你的代码中有这个:

System.out.println(addition + " has the length of " + total);

相反,您可以创建一个可以使用两个整数的方法:

public void printStatus(int check, int length) {
    System.out.println(check + " has the length of " + length);
}

允许你拨打电话

printStatus(addition, total);

这只是一个粗略的例子,但您可以在方法中包装代码的“过程”,并将执行该方法所需的必要参数传递给它。

答案 1 :(得分:-3)

package fifthAssignment;

public class Arithmetic {

    public static void main(String[] args) {

        // setting up the variable firstNumber and secondNumber
        int length = args.length;

        if (length != 3) {
            System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
            return;
        }
        int firstNumber = Integer.parseInt(args[0]);
        int secondNumber = Integer.parseInt(args[1]);
        int addition = firstNumber + secondNumber;
        int minus = firstNumber - secondNumber;
        int division = firstNumber / secondNumber;
        int multiply = firstNumber * secondNumber;
        String arithmetic = args[2];

        // following prints out to the console what the length of each argument
        // is.
        System.out.println(args[0] + " has the length of " + args[0].length());
        System.out.println(args[1] + " has the length of " + args[1].length());
        performOperation(arithmetic);
    }

    public void performOperation(String arithmetic) {
        if (arithmetic.equals("+")) {
            int total = String.valueOf(addition).length();
            System.out.println(addition + " has the length of " + total);
        } else if (arithmetic.equals("-")) {
            int total = String.valueOf(minus).length();
            System.out.println(minus + " has the length of " + total);
        } else if (arithmetic.equals("/")) {
            int total = String.valueOf(division).length();
            System.out.println(division + " has the length of " + total);
        } else if (arithmetic.equals("x")) {
            int total = String.valueOf(multiply).length();
             System.out.println(multiply + " has the length of " + total);
        }
    }

}