예제 8월 13일
2009. 8. 17. 01:14
class 가산기 { public static void main(String[] args) { System.out.println("결과: " + (new Integer(args[0]) + new Integer(args[1]))); } } /* run: java 가산기 50 60 */
/* 두수를 입력받아 큰 수를 출력하는 프로그램 (실행) java GT 68 78 (출력) 78이 68보다 큽니다. /* class GT { public static main(String[] args) { if (Integer.parseInt(args[0]) > Integer.parseInt(args[1])) { System.out.println(args[0] + "은 " + args[1] + "보다 큽니다."); } else { System.out.println(args[1] + "은 " + args[0] + "보다 큽니다."); } } } class GT { public static main(String[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println(((a > b) ? a : b) + "은 " + ((a < b) ? a : b) + "보다 큽니다."); } }
/* 세 수를 입력받아 가장 큰 수를 출력 (실행) java Max 10 15 3 (출력) 10, 15, 3 중 가장 큰 수는 15입니다. */ class Max { public static void main(String[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); int k = Integer.parseInt(args[2]); int max = -9999; if (i > j) { if (i > k) { max = i; } else { max = k; } } else { if (j > k) { max = j; } else { max = k; } } System.out.println(i + ", " + j + ", " + k + "중 가장 큰수는" + max + "입니다."); } }
class Max2 { public static void main(String[] args) { int number = Integer.parseInt(args[0]); int max = number; for (int i = 1; i < args.length; i++) { number = Integer.parseInt(args[i]); // if (max < number) max = number; max = (max < number) ? number : max ; } System.out.println("최대값 : " + max); } }
class 계산기 { public static void main(String[] args) { int result = 0; if (args[1].equals("+")) { result = new Integer(args[0]) + new Integer(args[2]); } else if (args[1].equals("-")) { result = new Integer(args[0]) - new Integer(args[2]); } else if (args[1].equals("*")) { result = new Integer(args[0]) * new Integer(args[2]); } else { result = new Integer(args[0]) / new Integer(args[2]); } System.out.prinln(args[0] + " " + args[1] + " " + args[2] + " = " + result); } }