Eggs Sunny Side Up
본문 바로가기
Algorithm/Java Festival

[Java Festival]

by guswn100059 2023. 3. 11.

lf문

Scanner sc = new Scanner(System.in);

		System.out.print("첫자리 0을 제외한 숫자를 입력해주세요 >> ");
		String num = sc.next();

		String[] number = num.split("");

		int sum = 0;

		for (int i = 0; i < number.length; i++) {
			if (number[i].equals("0")) {
				sum += 6;
			} else if (number[i].equals("1")) {
				sum += 2;
			} else if (number[i].equals("2")) {
				sum += 5;
			} else if (number[i].equals("3")) {
				sum += 5;
			} else if (number[i].equals("4")) {
				sum += 4;
			} else if (number[i].equals("5")) {
				sum += 5;
			} else if (number[i].equals("6")) {
				sum += 6;
			} else if (number[i].equals("7")) {
				sum += 3;
			} else if (number[i].equals("8")) {
				sum += 7;
			} else if (number[i].equals("9")) {
				sum += 6;
			}
		}
		System.out.println("대시('-')의 총합 >> "+sum);

 

Switch

import java.util.Scanner;

public class Me_25번 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

		System.out.print("첫자리 0을 제외한 숫자를 입력해주세요 >> ");
		String num = sc.next();

		String[] number = num.split("");

		int sum = 0;
		
		for(int i = 0; i < number.length; i++) {
			switch(number[i]) {
			case "0":
			case "9":
			case "6":
				sum += 6;
				break;
			
			case "1" :
				sum += 2;
				break;
				
			case "2":
			case "3":
			case "5":
				sum += 5;
				break;
				
			case "4":
				sum += 4;
				break;
				
			case "7":
				sum += 3;
				break;
				
			case "8":
				sum += 7;
				break;	
			}
		}
		System.out.print(sum);

	}

}

댓글