주어진 코드를 사용하여 지침/함수를 사용할 수있게되어 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);
}
}
}
이것은 기본 자바입니다. 자신이 직접 할 수있는 방법을 알아 보려면 기본 자습서를 거쳐야 할 수도 있습니다. – AntonH
"메서드/함수 내에서 수행 할 수있는 일반적인 작업이있는 곳에서 코드를 업데이트하고 싶습니다." –
또한 Java에서는 "함수"가 아니며 "메소드"라고합니다. – AntonH