01. 동전 교환
º 입력된 액수만큼 500원, 100원, 50원, 10원짜리 동전으로 교환해주는 프로그램
- 동전의 총 개수는 최소화할 것
- 고액의 동전을 우선적으로 교환해 줄 것
import java.util.Scanner;
public class ChangeEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int money, c1, c2, c3, c4;
System.out.println(" 교환할 금액을 입력 : ");
money = s.nextInt();
c1 = money / 500;
money = money % 500;
c2 = money / 100;
money = money % 100;
c3 = money / 50;
money = money % 50;
c4 = money / 10;
money = money % 10;
System.out.printf("\n 오백원 ==> %d개", c1);
System.out.printf("\n 백원 ==> %d개", c2);
System.out.printf("\n 오십원 ==> %d개", c3);
System.out.printf("\n 십원 ==> %d개", c4);
System.out.printf("\n 잔액 ==> %d개", money);
s.close();
}
}
02. 윤년 판별
º 사용자에게 특정 연도를 입력 받아 입력된 연도가 윤년인지 판단
- 윤년이란?
- 년도가 4로 나누어 떨어지는 해는 윤년이다. (2012년, 2016년, 2020년, ...) - 이 중에서 100으로 나누어 떨어지는 해는 평년이다. (1900년, 2100년, 2200년, ...) - 그 중에서 400으로 나누어 떨어지는 해는 윤년이다. (1600년, 2000년 2400년, ...) |
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int year;
System.out.println("연도 입력: ");
year = s.nextInt();
if(((year%4 == 0) && (year%10 != 0)) || (year%400 == 0))
System.out.printf("%d 년은 윤년!\n", year);
else
System.out.printf("%d 년은 평년!\n, year");
s.close();
}
}
03. 홀수 짝수 판별
º 키보드에서 입력받은 정수가 홀수인지 짝수인지 판별
- 홀수는 2로 나누었을 때 나머지가 1인 경우
- 짝수는 2로 나누었을 때 나머지가 0인 경우
- 나머지 연산자(%)를 이용
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
int num;
Scanner s = new Scanner(System.in);
System.out.println("정수 입력: ");
num = s.nextInt();
if(num%2 == 0) {
System.out.println("짝수!");
}else {
System.out.println("홀수!");
}
s.close();
}
}
04. 가위 바위 보 게임
º 사용자가 가위 바위 보 중에서 하나를 선택하면 이것을 컴퓨터가 생성한 난수값과 비교하여 누가 이겼는지를 화면에 출력
※자바에서 난수 생성 방법
- Random 클래스 활용 : boolean, int, long, float, double 난수를 얻을 때 사용
- Math 클래스 활용 : Math.random() 메소드는 0.0에서 1사이의 double 난수를 얻는데만 사용
import java.util.Scanner;
public class RockPaperEx {
//상수 선언
final int SCISSOR = 0; //가위(0)
final int ROCK = 1; //바위(1)
final int PAPER = 2; //보(2)
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("가위(0), 바위(1), 보(2)를 선택하세요: ");
int user = s.nextInt();
int computer = (int)(Math.random()*3);
if(user==0)System.out.print("가위");
else if(user==1)System.out.print("바위");
else System.out.print("보");
if(computer==0)System.out.println(": 가위");
else if(computer==1)System.out.println(": 바위");
else System.out.println(": 보");
if(user==computer)
System.out.println("비겼네요!");
else if(user==(computer+1)%3) //0은 1에게 지고, 1은 2에게 지고, 2는 0에게 진다.
System.out.println("이겼네요!");
else
System.out.println("안타깝게 지셨군요!");
s.close();
}
}
05. 출생년대와 성별 출력
º 사용자에게 주민등록번호 7번째 자리를 입력받아 출생년대와 성별을 출력
import java.util.Scanner;
public class JuminEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
byte num = -1; //주민번호 음수로 초기화
short year = 0; //태어난 년도
String gender = ""; //성별
System.out.println("주민번호의 7번째 자리를 입력 : ");
num = s.nextByte();
switch(num) {
case 9:
case 0:
year = 1000;
break;
case 1:
case 2:
year = 1900;
break;
case 3:
case 4:
year = 2000;
break;
default: year = -1;
}
if (year !=-1) {
gender = num%2 == 0 ? "여성" : "남성";
}
s.close();
System.out.println();
System.out.println(year+"년대 "+gender+"분입니다!");
}
}
06. 계절 출력
º 1~12월 사이의 월을 입력 받아 봄, 여름, 가을, 겨울을 판단
import java.util.Scanner;
public class SeasonEx {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
System.out.println("월(1~12)입력 : ");
int month = a.nextInt();
switch(month) {
case 3: case 4: case 5:
System.out.println("Spring!");
break;
case 6: case 7: case 8:
System.out.println("Summer!");
break;
case 9: case 10: case 11:
System.out.println("Autumn!");
break;
case 12: case 1: case 2:
System.out.println("Winter!");
break;
default: System.out.println("잘못된 월을 입력하셨습니다!");
a.close();
}
}
}
07. 커피 메뉴 가격
º switch문을 이용하여 커피 메뉴의 가격을 알려주는 프로그램
- 에스프레소, 카푸치노, 카페라떼 : 3500원
- 아메리카노 : 2000원
import java.util.Scanner;
public class CoffeeEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("무슨 커피를 주문하시겠습니까?");
String order = s.next();
int price = 0;
switch(order) {
case "에스프레소":
case "카푸치노":
case "카페라떼":
price = 3500; break;
case "아메리카노":
price = 2000; break;
default: System.out.println("메뉴에 없습니다!");
}
if(price!=0) System.out.println(order+"는 "+ price + "원입니다!");
s.close();
}
}
08. 출생년도별 띠 출력
º 출생년도를 입력받아 출생년도에 해당하는 띠 출력
import java.util.Scanner;
public class FortuneEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int year;
System.out.println("출생년도 입력: ");
year = s.nextInt();
switch(year%12) {
case 0:System.out.println("원숭이띠");
break;
case 1:System.out.println("닭띠");
break;
case 2:System.out.println("개띠");
break;
case 3:System.out.println("돼지띠");
break;
case 4:System.out.println("쥐띠");
break;
case 5:System.out.println("소띠");
break;
case 6:System.out.println("호랑이띠");
break;
case 7:System.out.println("토끼띠");
break;
case 8:System.out.println("용띠");
break;
case 9:System.out.println("뱀띠");
break;
case 10:System.out.println("말띠");
break;
case 11:System.out.println("양띠");
break;
}
s.close();
}
}
09. for문을 이용한 합산 출력
º for문을 이용하여 1부터 10까지 덧셈을 표시하고 합을 구하는 프로그램
public class ForSample {
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<=10; i++) { //1~10까지 반복
sum += i;
System.out.print(i); //더하는 수 출력
if(i<=9)
System.out.print("+"); //1~9까지는 '+' 출력
else { //i가 10인 경우
System.out.print("="); //'=' 출력
System.out.print(sum); //덧셈 결과 출력
}
}
}
}
10. break문 활용한 while문
º "exit"이 입력되면 while문을 벗어나도록 break문을 활용하는 프로그램
import java.util.Scanner;
public class BreakEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("exit를 입력하면 종료!");
while(true) {
System.out.print(">>데이터 입력 => ");
String text = s.nextLine();
if(text.equals("exit")) //"exit"가 입력되면 반복 종료
break; //while 문을 벗어남
}
System.out.println("종료합니다!");
s.close();
}
}
'JAVA > 자바 실습' 카테고리의 다른 글
Java | Lab5 (0) | 2021.04.15 |
---|---|
Java | Lab4_Ex (0) | 2021.04.12 |
Java | Lab4 (0) | 2021.04.12 |
Java | Lab2 (0) | 2021.04.11 |
Java | Lab1 (0) | 2021.04.11 |