Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 반응형 웹 프로젝트
- rnn
- 예제
- 상속
- 파이썬
- 푸리에 변환
- FileWriter
- Codility
- HTML
- FlowLayout
- CSS
- 메서드
- g검정
- oracle
- ObjectOutputStream
- Database
- iframe 태그
- 사전학습
- BorderLayout
- inline
- Position
- 반응형웹
- 미디어쿼리
- html 프로젝트
- java
- 퍼셉트론
- html 기초
- GridLayout
- css 기초
- FFT
Archives
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 17일차 본문

메서드를 사용하지 않고 작성한 코드를 메서드를 이용하여 바꾸는 예제
예제1
<Original Code>
import java.util.Scanner;
public class Prac4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 선언
double jumsu1;
double jumsu2;
String yun;
double result = 0;
// 입력
System.out.print("첫번째수:");
jumsu1 = sc.nextDouble();
System.out.print("두번째수:");
jumsu2 = sc.nextDouble();
System.out.print("연산자:");
yun = sc.next();
// 연산
switch (yun) {
case "+":
result = jumsu1 + jumsu2;
break;
case "-":
result = jumsu1 - jumsu2;
break;
case "*":
result = jumsu1 * jumsu2;
break;
case "/":
result = jumsu1 / jumsu2;
break;
}
// 출력
System.out.println();
System.out.printf("%f %s %f=%f", jumsu1, yun, jumsu2, result);
}
}
<Method Code>
import java.util.Scanner;
public class Prac4 {
static double input_jumsu1() {
Scanner sc = new Scanner(System.in);
System.out.print("첫번째수:");
double jumsu1 = sc.nextDouble();
return jumsu1;
}
static double input_jumsu2() {
Scanner sc = new Scanner(System.in);
System.out.print("두번째수:");
double jumsu2 = sc.nextDouble();
return jumsu2;
}
static String input_yun() {
Scanner sc = new Scanner(System.in);
System.out.print("연산자:");
String yun = sc.next();
return yun;
}
static double calc(double jumsu1, double jumsu2, String yun) {
double result=0;
switch (yun) {
case "+":
result = jumsu1 + jumsu2;
break;
case "-":
result = jumsu1 - jumsu2;
break;
case "*":
result = jumsu1 * jumsu2;
break;
case "/":
result = jumsu1 / jumsu2;
break;
}
return result;
}
static void output(double jumsu1, double jumsu2, String yun, double result) {
System.out.println();
System.out.printf("%f %s %f = %f", jumsu1, yun, jumsu2, result);
}
public static void main(String[] args) {
// 선언
double jumsu1;
double jumsu2;
String yun;
double result = 0;
// 입력
jumsu1 = input_jumsu1();
jumsu2 = input_jumsu2();
yun = input_yun();
// 연산
result = calc(jumsu1, jumsu2, yun);
// 출력
output(jumsu1, jumsu2, yun, result);
}
}
예제2
<Original Code>
import java.util.Scanner;
public class Prac5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 선언 : 변수만들기
int kor = 0, eng = 0;
int total = 0;
double avg = 0;
char credit = 0;
// 입력 : 변수에 데이터 저장하기
System.out.print("국어점수 입력 : ");
kor = sc.nextInt();
System.out.print("영어점수 입력 : ");
eng = sc.nextInt();
// 연산 : 데이터 가공하기
total = kor + eng;
avg = (double)total / 2;
if (avg >= 90) credit = 'A';
else if (avg >= 80) credit = 'B';
else if (avg >= 70) credit = 'C';
else if (avg >= 60) credit = 'D';
else credit = 'F';
// 출력 : 변수에 데이터와 결과값 확인하기
System.out.println("총점 : " + total);
System.out.println("평균 : " + avg);
System.out.println("학점 : " + credit);
}
}
<Method Code>
import java.util.Scanner;
public class Prac5 {
static int input_kor() {
Scanner sc = new Scanner(System.in);
System.out.print("국어점수 입력 : ");
int kor = sc.nextInt();
return kor;
}
static int input_eng() {
Scanner sc = new Scanner(System.in);
System.out.print("영어점수 입력 : ");
int eng = sc.nextInt();
return eng;
}
static int calc_tot(int kor, int eng) {
int total = kor + eng;
return total;
}
static double calc_avg(int total) {
double avg = (double)total / 2;
return avg;
}
static char calc_credit(double avg) {
char credit = 0;
if (avg >= 90) credit = 'A';
else if (avg >= 80) credit = 'B';
else if (avg >= 70) credit = 'C';
else if (avg >= 60) credit = 'D';
else credit = 'F';
return credit;
}
static void output(int total, double avg, char credit) {
System.out.println("총점 : " + total);
System.out.println("평균 : " + avg);
System.out.println("학점 : " + credit);
}
public static void main(String[] args) {
// 선언 : 변수만들기
int kor = 0, eng = 0;
int total = 0;
double avg = 0;
char credit = 0;
// 입력 : 변수에 데이터 저장하기
kor = input_kor();
eng = input_eng();
// 연산 : 데이터 가공하기
total = calc_tot(kor, eng);
avg = calc_avg(total);
credit = calc_credit(avg);
// 출력 : 변수에 데이터와 결과값 확인하기
output(total, avg, credit);
}
}
예제3
<Original Code>
import java.util.Scanner;
public class Prac6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String result; // 계속할 지 여부 저장
int num; // 단 저장
while (true) {
// 입력
System.out.print("몇 단을 출력할지 입력하세요: ");
num = sc.nextInt();
// 연산, 출력
for (int i = 1; i < 10; i++) {
System.out.printf("%d * %d = %d", num, i, num * i);
System.out.println();
}
// while 탈출 코드
System.out.print("계속할지 선택하세요 (y:계속) : ");
result = sc.next();
if (!result.equals("y")) {
System.out.println("프로그램 종료");
System.exit(0);
}
}
}
}
<Method Code>
import java.util.Scanner;
public class Prac6 {
static int input_dan() {
Scanner sc = new Scanner(System.in);
System.out.print("몇 단을 출력할지 입력하세요: ");
int num = sc.nextInt();
return num;
}
static void output_gugudan(int num) {
for (int i = 1; i < 10; i++) {
System.out.printf("%d * %d = %d", num, i, num * i);
System.out.println();
}
}
static String input_esc() {
Scanner sc = new Scanner(System.in);
System.out.print("계속할지 선택하세요 (y:계속) : ");
String result = sc.next();
return result;
}
static void end_pgm(String result) {
if (!result.equals("y")) {
System.out.println("프로그램 종료");
System.exit(0);
}
}
public static void main(String[] args) {
String result; // 계속할 지 여부 저장
int num; // 단 저장
while (true) {
// 입력
num = input_dan();
// 연산, 출력
output_gugudan(num);
// while 탈출 코드
result = input_esc();
end_pgm(result);
}
}
}'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 19 - 21일차 (0) | 2022.10.17 |
|---|---|
| [Java/Eclipse] Java 18일차 (0) | 2022.10.11 |
| [Java/Eclipse] Java 15, 16일차 (0) | 2022.10.06 |
| [Java/Eclipse] Java 14일차 (1) | 2022.10.04 |
| [Java/Eclipse] Java 13일차 (0) | 2022.09.30 |