Quiz) 커스텀메서드

🔓 급여 구하기

package pack_CusMtd01;

import java.util.Scanner;

public class Salary {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("세전급여를 입력해 주세요(단위. 원) : ");
		int money = scanner.nextInt();
		
		scanner.close();
		
		mtd_Salary(money);
	}

	public static void mtd_Salary(int money) {
		
		double salary;
		salary = money - (money * 0.033);
		System.out.printf("실지급액 : %.0f", salary);
	}
}




🔓 환전액 구하기

package pack_CusMtd02;

import java.util.Scanner;

public class Exchange {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("환전액 입력 : ");
		int money = scanner.nextInt();
		
		scanner.close();
		
		mtd_Change(money);
	}

	public static void mtd_Change(int money) {
		
		int dollar = 1200;
		double exchange = (double)money / dollar;
		System.out.printf("환전 후 지급액 : %.1f", exchange);
	}
}




🔓 누적 계산하기

package pack_CusMtd03;

import java.util.Scanner;

public class Sum {

	public static void main(String[] args) {

	Scanner scanner = new Scanner(System.in);
	
	System.out.print("2개의 값을 입력하세요 : ");
	int num1 = scanner.nextInt();
	int num2 = scanner.nextInt();
	
	scanner.close();
	
	int start = num1;
	int end = num2;
	
	if (num1 > num2) {
		start = num2;
		end = num1;
	}
	
	int sum = mtd_Sum(start, end);
	
	System.out.printf("%d부터 %d까지의 누적 합 : %d", start, end, sum);
	}

	public static int mtd_Sum(int start, int end) {
		
		int sum = 0;
		
		for (int i = start; i <= end; i++) {
			sum += i;
		}
		
		return sum;
	}
}




🔓 가장 작은 값 찾기

package pack_CusMtd04;

import java.util.Scanner;

public class SmallNum {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("임의의 숫자 다섯 개를 입력하세요 : ");
		
		double[] num = new double[5];
		for (int i = 0; i < num.length; i++) {
			num[i] = scanner.nextDouble();
		}
		
		scanner.close();
		
		double min = mtd_FindSmall(num);
		
		System.out.println("가장 작은 값 : " + min);
	}
	
	public static double mtd_FindSmall(double[] num) {
		
		double min = num[0];
		
		for (int i = 0; i < num.length; i++) {
			if (num[i] < min) {
				num[i] = min;
			}
		}
		return min;
	}

}




🔓 숫자 입력 받아서 아스테리스크 사각형 만들기

package pack_CusMtd05;

import java.util.Scanner;

public class Shape {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("숫자를 입력해 주세요(1~10) : ");
		int num = scanner.nextInt();
		
		scanner.close();
		
		if (num<1 || num>10) {
			System.out.println("1이상 10이하의 정수만 입력가능함");
			System.exit(0);
		}
		
		mtd_Shape(num);
	}
	
	public static void mtd_Shape(int num) {
		
		for (int i = 0; i < num; i++) {
			
			for (int j = 0; j < num; j++) {
				
				System.out.print(" * ");
				
			}	System.out.println();
		}
	}

}




🔓 숫자 역순 출력하기

package pack_CusMtd06;

import java.util.Scanner;

public class ReverseArray {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("배열 크기 설정 : ");
		int len = scanner.nextInt();
		int[] num = new int[len];
		
		System.out.printf("입력하실 숫자 %d개를 입력해주세요 : ", len);
		for (int i = 0; i < num.length; i++) {
			num[i] = scanner.nextInt();
		}
				
		scanner.close();
		
		mtdReverse(num);
	}
	
	public static void mtdReverse(int[] num) {
		
		System.out.println("입력하신 숫자들의 역순은 다음과 같습니다.");
		
		for (int i = num.length - 1; i >= 0; i--) {
			System.out.print(num[i] + " ");
		}
	}

}




🔓 홀수 개수 구하기

package pack_CusMtd07;

import java.util.Scanner;

public class CntOdd {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("숫자 2개를 입력하세요 : ");
		int num1 = scanner.nextInt();
		int num2 = scanner.nextInt();
		
		scanner.close();
		
		mtdOdd(num1, num2);
	}

	public static void mtdOdd(int num1, int num2) {
		
		int cnt = 0;
		
		for (int i = num1; i <= num2; i++) {
			if (i % 2 == 1) {
				cnt++;
			}
			
		}
		
		System.out.printf("%d에서 %d까지의 홀수 갯수 : %d",
        					num1, num2, cnt);
	}
}




🔓 BMI 구하기

package pack_CusMtd08;

import java.util.Scanner;

public class ChkBMI {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("성별을 입력하세요(m/f) : ");
		String gender = scanner.next();
		System.out.print("몸무게를 입력하세요(kg) : ");
		double weight = scanner.nextDouble();
		System.out.print("키를 입력하세요(cm) : ");
		double height = scanner.nextDouble();
		
		scanner.close();
		
		mtd_Bmi(gender, weight, height);
	}
	
	public static void mtd_Bmi
				(String gender, double weight, double height) {
		height /= 100;
		
		double bmi = weight / Math.pow(height, 2);
		System.out.printf("당신의 BMI는 : %.1f입니다.", bmi);
		
		String txt = "";
		
		if (gender == "m"){
			if (bmi >= 28.0) {
				txt = "과체중";
			} else if (bmi >= 20){
				txt = "표준체중";
			} else {
				txt = "저체중";
			}
			
		} else if (gender == "f"){
			if (bmi >= 26.0) {
				txt = "과체중";
			} else if (bmi >= 18.5){
				txt = "표준체중";
			} else {
				txt = "저체중";
			}
		}
		
		System.out.println(txt + "입니다.");

		
	}

}

좋은 웹페이지 즐겨찾기