본문 바로가기
공부/과제.

JAVA 문제풀이 14

by 햄이; 2021. 5. 7.

 

 

Q1. 사칙연산을 하는 함수 프로그램 만들기

 

package Day10;

import java.util.Scanner;

/*
 * Q.21
 * 사칙연산을 하는 함수 프로그램을 만드세요
 * 1.연산자를 입력하세요
 * 2. 숫자 1입력
 * 3. 숫자 2입력
 * 4. 결과 값은 XX입니다.
 * */
public class Question {
	
	//주고 받고
	public static int plus(int a, int b) {
		return (a+b);	
	}
	
	//안주고 받고
	public static void minus(int a, int b) {
		System.out.println("뺄셈의 결과는 "+(a-b)+"입니다");
	}
	
	//주고 안받고
	public static int multiply() {
		Scanner sc = new Scanner(System.in);
		System.out.println("숫자 1 입력 : ");
		int a = sc.nextInt();
		System.out.println("숫자 2 입력 :");
		int b = sc.nextInt();
		int result = a*b;
		
		return result;
	}
	
	//안주고 안받고
	public static void divide() {
		Scanner sc = new Scanner(System.in);
		System.out.println("숫자 1 입력 : ");
		int a = sc.nextInt();
		System.out.println("숫자 2 입력 :");
		int b = sc.nextInt();
		if(b!=0) {
			System.out.print("나눗셈의 결과는 " + (a/b) +"입니다.");	
		}else {
			System.out.println("0으로 나눌 수 없습니다.");
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		System.out.println("연산자를 입력하세요");
		System.out.println(" +  |  -  |  *  |  /   ");
		String choice = sc.next();
		
		
		if(choice.equals("+") || choice.equals("-") || choice.equals("*") || choice.equals("/")) {
			
			

			
			switch(choice) {
			case "+" : 
				System.out.println("첫 번째 숫자 입력 : ");
				int a = sc.nextInt();
				System.out.println("두 번째 숫자 입력 : ");
				int b = sc.nextInt();
				int result = plus(a,b);
				System.out.println("덧셈의 결과는 "+result+"입니다");
				break;
			case "-" : 
				System.out.println("첫 번째 숫자 입력 : ");
				int c = sc.nextInt();
				System.out.println("두 번째 숫자 입력 : ");
				int d = sc.nextInt();
				minus(c,d);
				break;
			case "*" : 
				int m = multiply();
				System.out.println("곱셈의 결과는 "+m+"입니다.");
				break;
			case "/" : 
				divide();
				break;
			}
			
		}else {
			System.out.println("잘못된 연산자 입력입니다.");
		}
		
	}

}

위 실행 결과

 

 

Q2. 핸드폰 숫자 자판 만들기 프로그램

package Day11;

import java.util.Scanner;

/*
 * Q.22
 * 1,*,0,#은 그대로 출력
 * 함수 두개이상 / 무한반복
 * QUIT 되면 종료 = 77 88 444 8 눌러야함
 * 중복 알파벳 출력 불가능 22222 누르면 b나옴
 * */
public class Explain1 {

	public static String input()
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		return str;
	}

	public static String makeString(String str)
	{
		char[][] ch = {
				{'1'},//1
				{'A', 'B', 'C'}, // 2
				{'D', 'E', 'F'}, // 3
				{'G', 'H', 'I'}, // 4
				{'J', 'K', 'L'}, // 5
				{'M', 'N', 'O'}, // 6
				{'P', 'Q', 'R', 'S'}, // 7
				{'T', 'U', 'V'}, // 8
				{'W', 'X', 'Y', 'Z'}, // 9
				{'*'},//*
				{'0'},//0
				{'#'}//#
		};

		String make = "";

		int i = 0;
		while(i != str.length())
		{
			if(str.charAt(i) == '1' || str.charAt(i) == '*' || 
					str.charAt(i) == '0' ||str.charAt(i) == '#')
			{
				make += str.charAt(i);
				i++;
			}
			else
			{
				int count = 0;

				System.out.println(i);

				while(i+count < str.length()&&str.charAt(i) == str.charAt(i+count))
				{
					count++;
				}

				System.out.println(count);
				//						
				if(str.charAt(i) == '9' || str.charAt(i) == '7')
				{

					int num = (str.charAt(i) - '0') -1;
					make+=ch[num][(count-1)%4];

				}
				else
				{
					int num = (str.charAt(i) - '0') -1;
					make+=ch[num][(count-1)%3];
				}

				i += count;

				System.out.println(make);
			}



		}
		return make;
	}

	public static void output()
	{

		while(true)
		{			
			String make = makeString(input());

			System.out.println("결과 값 : "+make+"입니다.");


			if(make.equals("QUIT"))
			{

				System.out.println(make+"입니다. 프로그램을 종료합니다.");
				break;
			}
		}
	}


	public static void main(String[] args) {

		output();

	}
}

 

 

위 실행 결과

 

'공부 > 과제.' 카테고리의 다른 글

JAVA 문제풀이 16  (0) 2021.05.07
JAVA 문제풀이 15  (0) 2021.05.07
JAVA 문제풀이 13 배열 낚시 게임  (0) 2021.04.28
JAVA 문제풀이 12 버블정렬  (0) 2021.04.26
JAVA 문제풀이 11  (0) 2021.04.26

댓글