2014-04-22 6 views
-2

주어진 코드를 사용하여 지침/함수를 사용할 수있게되어 Java를 두 번 이상 사용할 수있는 프로 시저를 저장할 수 있으므로 Java 코드가있는 곳에서 코드를 업데이트하고 싶습니다. 일반적인 작업은 메소드/함수 내에서 수행 될 수 있습니다. 어떤 생각을 어떻게 할 것인가?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); 
     } 

    } 
} 
+6

이것은 기본 자바입니다. 자신이 직접 할 수있는 방법을 알아 보려면 기본 자습서를 거쳐야 할 수도 있습니다. – AntonH

+3

"메서드/함수 내에서 수행 할 수있는 일반적인 작업이있는 곳에서 코드를 업데이트하고 싶습니다." –

+2

또한 Java에서는 "함수"가 아니며 "메소드"라고합니다. – AntonH

답변

2

나는 단 한 예를 제공 하겠지만, 독자적으로해야합니다.

당신은 당신의 코드에서이 있습니다

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

대신, 당신은 잠재적으로 두 가지의 int로 작업 할 방법을 만들 수 있습니다

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

수있는 것입니다 당신이 전화를

printStatus(addition, total); 

이것은 간단한 예일 뿐이지 만 메서드에서 코드의 "프로세스"를 래핑하고 메소드를 실행하는 데 필요한 매개 변수.

-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); 
     } 
    } 

} 
+1

OP에 코드를 다시 붙여 넣었습니까? – Rogue

+0

메서드 내에서 작업 논리를 추가하여 솔루션을 다시 게시했습니다. 우리가 어떻게 모듈성을 달성했는지. –

+0

코드를 수정했지만 코드의 전체 덩어리를 다른 곳에 두는 것과 크게 다르지 않습니다. 실제로 모듈화 된 것은 아니며 두 개로 나뉩니다. – Rogue