햄발
함수와 만들기 (연습문제) 본문
풀이1
package basic.ch06;
import java.util.Scanner;
public class FunctionExercise {
// 코드의 시작점
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Systehttp://m.out.print("이름를 입력해 주세요 :");
String greet = sc.next();
Greet(greet);
System.out.println("--------------------------");
Systehttp://m.out.print("아무 숫자를 입력해 주세요 :");
int squareNum = sc.nextInt();
System.out.println(squareNum +"의 제곱은 : "+Square(squareNum));
System.out.println("--------------------------");
Systehttp://m.out.print("나이를 입력해 주세요 :");
int age = sc.nextInt();
System.out.println("당신은 성인이 " + CheckAdult(age));
System.out.println("--------------------------");
Systehttp://m.out.print("아무 숫자를 입력해 주세요 :");
int signOfNumber = sc.nextInt();
System.out.println("당신이 입력한 수는 : " + SignOfNumber(signOfNumber));
System.out.println("--------------------------");
Systehttp://m.out.print("첫 번째 숫자를 입력하세요 : ");
int num1 = sc.nextInt();
Systehttp://m.out.print("두 번째 숫자를 입력하세요 : ");
int num2 = sc.nextInt();
System.out.println("두 수중 큰 수는 : " + FindMax(num1, num2));
System.out.println("--------------------------");
} // end of main
static void Greet(String greet) {
System.out.println("안녕하세요 " + greet + "님!");
}
static int Square(int a) {
int square;
square = a*a;
return square;
}
static boolean CheckAdult(int a) {
boolean checkAdult;
if (a >= 18) {
checkAdult = true;
} else {
checkAdult = true;
}
return checkAdult;
}
static String SignOfNumber (int a) {
String numType;
if (a > 0) {
numType = "Positive";
} else if (a < 0) {
numType = "negative";
} else {
numType = "zero";
}
return numType;
}
static int FindMax(int a, int b) {
int result = a > b ? a : b;
return result;
}
} // end of class
풀이2
package basic.ch06;
import java.util.Scanner;
public class Practice {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("안녕하세요 " + sysprint() + "님!");
System.out.println("입력한 수 의 제곱은 " + square() + "입니다.");
System.out.println("당신은 성인이 : " + ageCount());
System.out.println("정수 입력 : ");
int a = sc.nextInt();
System.out.println("입력한 정수는 : " + signOfNumber(a));
System.out.println("숫자 5개 입력하셈");
int[] num = new int[5];
for (int i = 0; i < 5; i++) {
num[i] = sc.nextInt();
}
System.out.println("입력한것중 젤큰거는 : " + findMax(num));
}
static String sysprint() {
System.out.println("이름 입력");
String result = sc.next();
return result;
}
static int square() {
System.out.println("정수 하나 입력");
int result = sc.nextInt();
return result * result;
}
static boolean ageCount() {
System.out.println("나이 입력하세요");
int age = sc.nextInt();
boolean result;
if (age < 18) {
result = false;
} else {
result = true;
}
return result;
}
static String signOfNumber(int num) {
String result;
if (num == 0) {
result = "zero";
} else if (num < 0) {
result = "negative";
} else {
result = "positive";
}
return result;
}
static int findMax(int[] getNums) {
int max = getNums[0];
for (int i = 0; i < getNums.length; i++) {
if(getNums[i] > max) {
max = getNums[i];
}
}
return max;
}
}
'Java' 카테고리의 다른 글
RunTime Data Area (0) | 2024.04.16 |
---|---|
메소드(method)와 변수 (0) | 2024.04.15 |
함수와 메서드 (0) | 2024.04.15 |
객체 값 할당하기 (0) | 2024.04.15 |
클래스 와 객체 (0) | 2024.04.15 |