도라에몽주머니

[Java/Eclipse] Java 14일차 본문

Study/Java

[Java/Eclipse] Java 14일차

에몽쓰 2022. 10. 4. 16:59

Java

 

예제

점수를 입력받아서 등수를 계산해보자.

 

<실행 결과>

1번 점수 입력 : 70

2번 점수 입력 : 80

3번 점수 입력 : 90

4번 점수 입력 : 85

5번 점수 입력 : 87

 

*** 결과 ***

70점 : 5등

80점 : 4등

90점 : 1등

85점 : 3등

87점 : 2등

import java.util.Scanner;

public class Prac3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] score = new int[5];
		int[] rank = new int[5];
		
		for(int i=0; i<score.length; i++) {
			System.out.print((i+1) + "번 점수 입력 : ");
			score[i] = sc.nextInt();
			rank[i] = 1;
		}
		
		for(int n=0; n<score.length; n++) {
			for(int m=0; m<score.length; m++) {
				if(score[n]<score[m]) {
					rank[n]++;
				} else if(score[n]>score[m]) continue;
			}
		}
		
		System.out.println();
		System.out.println("*** 결과 ***");
		for(int j=0; j<score.length; j++) {
			System.out.println(score[j] + "점 : " + rank[j] + "등");
		}
		
	}
}

 

예제

주차 관리 프로그램을 작성하세요.

- 주차는 5대를 할 수 있다.

- 주차된 곳 : true, 비어있는 곳 : false

 

<실행 결과>

주차 관리 프로그램

**********************

1. 입차

2. 출차

3. 리스트

4. 종료

**********************

메뉴 :

-- 1번인 경우
위치 입력 : 3
3위치에 입차 / 이미 주차되어있습니다
            (주차되었습니다) 

-- 2번인 경우
위치 입력 : 4
4위치에 출차 / 주차되어 있지않습니다
            (출차되었습니다)

-- 3번인 경우
1위치 : true
2위치 : false
3위치 : true
4위치 : false
5위치 : false

import java.util.Scanner;

public class Prac4 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		boolean[] parking = new boolean[5];
		int choice = 0;
		int loc = 0;
		
		System.out.println("주차 관리 프로그램");
		while(true) {
			System.out.println("**********************");
			System.out.println("1. 입차");
			System.out.println("2. 출차");
			System.out.println("3. 리스트");
			System.out.println("4. 종료");
			System.out.println("**********************");
			System.out.print("메뉴 : ");
			choice = sc.nextInt();
			
			if(choice==1) {
				System.out.print("위치 입력 : ");
				loc = sc.nextInt();
				
				if(parking[(loc-1)]==true) System.out.println("이미 주차되어있습니다.\n");
				else {
					parking[(loc-1)] = true;
					System.out.println(loc + "위치에 주차되었습니다.\n");
				}
				
			} else if(choice==2) {
				System.out.print("위치 입력 : ");
				loc = sc.nextInt();
				
				if(parking[(loc-1)]==false) System.out.println("주차되어 있지않습니다.\n");
				else {
					parking[(loc-1)] = false;
					System.out.println(loc + "위치에 출차되었습니다.\n");
				}
			} else if(choice==3) {
				for(int i=0; i<parking.length; i++) {
					System.out.printf("%d위치 : %b\n", (i+1), parking[i]);
				}
				System.out.println();
				
			} else if(choice==4) {
				System.out.println("프로그램을 종료합니다.");
				break;
			} else System.out.println("1~4 사이의 정수를 입력해주세요.\n");
		}
		
	}
}

'Study > Java' 카테고리의 다른 글

[Java/Eclipse] Java 17일차  (0) 2022.10.07
[Java/Eclipse] Java 15, 16일차  (0) 2022.10.06
[Java/Eclipse] Java 13일차  (0) 2022.09.30
[Java/Eclipse] Java 12일차  (0) 2022.09.29
[Java/Eclipse] Java 11일차  (0) 2022.09.28