도라에몽주머니

[Java/Eclipse] Java 13일차 본문

Study/Java

[Java/Eclipse] Java 13일차

에몽쓰 2022. 9. 30. 16:59

Java

 

예제

학생 5명의 성적을 입력한 후, 총점과 평균을 구해보자.

 

Class name: Prac1.java

 

<실행 결과>

1번 학생의 점수를 입력 : 79

2번 학생의 점수를 입력 : 74

3번 학생의 점수를 입력 : 89

4번 학생의 점수를 입력 : 85

5번 학생의 점수를 입력 : 97

 

총점 : 424, 평균 : 84.8

import java.util.Scanner;

public class Prac1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] score = new int[5];
		int total=0;
		
		for(int i=0; i<score.length; i++) {
			System.out.print((i+1) + "번 학생의 점수를 입력 : ");
			score[i] = sc.nextInt();
			total += score[i];
		}
		
		System.out.println();
		System.out.println("총점 : " + total + ", 평균 : " + (double)total/score.length);
		
	}
}

 

예제

1월 1일부터 입력한 날까지 일수를 구해보자

 

Class name: Prac2.java

 

<실행 결과>

*** 일수 구하기 ***

월 : 5

일 : 5

 

1월1일부터 5월5일까지는 125일 입니다.

import java.util.Scanner;

public class Prac2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		int month = 0;
		int day = 0;
		int total = 0;
		
		System.out.println("*** 일수 구하기 ***");
		System.out.print("월 : ");
		month = sc.nextInt();
		System.out.print("일 : ");
		day = sc.nextInt();
		
		for(int i=0; i<month-1; i++) {
			total += months[i];
		}
		
		total += day;
		System.out.println("\n1월1일부터 " + month + "월" + day + "일까지는 " + total + "일 입니다.");
		
		
	}
}

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

[Java/Eclipse] Java 15, 16일차  (0) 2022.10.06
[Java/Eclipse] Java 14일차  (1) 2022.10.04
[Java/Eclipse] Java 12일차  (0) 2022.09.29
[Java/Eclipse] Java 11일차  (0) 2022.09.28
[Java/Eclipse] Java 10일차  (0) 2022.09.27