Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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 31
Archives
Today
Total
관리 메뉴

Everything has an expiration date

037 - Java for문과 array를 이용하여 if문으로 짝수, 홀수의 합 구하는 프로그램 재구성 본문

[Java]/과제

037 - Java for문과 array를 이용하여 if문으로 짝수, 홀수의 합 구하는 프로그램 재구성

Jelly-fish 2023. 8. 30. 16:05
// for문, array 사용해서 Test037 재구성




import java.util.Scanner;


public class Mytest3
{
	public static void main(String[] args)
	{

		Scanner sc = new Scanner(System.in);
		

		System.out.print("5개의 정수 입력(공백 구분) : ");
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int d = sc.nextInt();
		int e = sc.nextInt();

		int[ ] inputData = { a,b,c,d,e };
		// ◆ 값으로 배열 생성       : String[ ] names = { “혼공자”, “혼공족장”, “자바맨” };
		// ◆ new 연산자로 배열 생성 : int[] intArray = new int[5];

		int evenSum = 0;
		int oddSum = 0;


		for (int i=0 ; i<5 ; i++)
		{
				
			if (inputData[i] % 2 == 0)
			{
				evenSum += inputData[i];
			}
			else
			{
				oddSum += inputData[i];
			}

		}

		System.out.printf("\n 짝수의 합 : %d, 홀수의 합 : %d \n", evenSum, oddSum);

	
		
	}
}