📌 06 

철수와 영희가 가위(s), 바위(r), 보(p) 게임을 한다. 다음 실행 결과와 같이 r,p,s 중 하나를 입력해 승자 또는 무승부를 출력하는 프로그램을 출력하는 프로그램을 작성하라.

 

철수 :  r
영희 : s 
철수 , 승!

scanner 클래스의 next() 메서드를 호출하면 키보드에서 입력된 문자열을 읽을 수 있다.문자열 타입은 equals() 메서드를 이용해 비교한다. 예를 들어 String s가 있을 때  s.equals("r")은 문자열 s와 'r'을 비교해 같다면 0, 다르면 0이 아닌 수를 반환한다.

 

<풀이>

package ch03;

import java.util.Scanner;

public class P123N6 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("철수 : ");
		String m = sc.nextLine();
		System.out.print("영희 : ");
		String w = sc.nextLine();		
		
		// 가위:s 바위:r 보:p
		if((m.equals("s") && w.equals("p")) || (m.equals("r") && w.equals("s")) || (m.equals("p") && w.equals("r"))) {//철수가 이겼을 경우
			System.out.print("철수,승!");
		}	
		else if((w.equals("s") && m.equals("p")) || (w.equals("r") && m.equals("s")) || (w.equals("p") && m.equals("r"))) { //철수가 이겼을 경우
			System.out.print("영희,승!");
		}
		else{
			System.out.print("비겼습니다.");
		}
	}

}

 

<출력 결과>

📌 07 

06에서 프롬프트와 r,p,s를 입력하는 부분, 입력된 데이터에 따라 승자를 출력하는 부분을 각각 메서드로 작성하라. main()메서드는 다음과 같다.

public static void main(String[] args) {
   String c = input("철수");
   String y = input("영희");
   whosWin(c,y);
}

06번에서 메서드를 활용해서 푸는 방법 

 

<풀이>

package ch03;

import java.util.Scanner;

public class P123N7 {

	public static void main(String[] args) {
		   String c = input("철수");
		   String y = input("영희");
		   whosWin(c,y);
		}
	
		//철수와 영희 이름을 받는 메서드 생성
		public static String input(String n) {
			Scanner sc = new Scanner(System.in);
			System.out.printf(n+": ");
			n = sc.next();
			
			return n;
		}
		
		// 가위:s 바위:r 보:p
		public static void whosWin(String c, String y) {
		if((c.equals("s") && y.equals("p")) || (c.equals("r") && y.equals("s")) || (c.equals("p") && y.equals("r"))) {//철수가 이겼을 경우
			System.out.print("철수,승!");
		}	
		else if((y.equals("s") && c.equals("p")) || (y.equals("r") && c.equals("s")) || (y.equals("p") && c.equals("r"))) { //철수가 이겼을 경우
			System.out.print("영희,승!");
		}
		else{
			System.out.print("비겼습니다.");
			}
		}	
}

 

출력 결과 

 

📌 08

다음과 같은 프로그램이 있다. factorial() 메서드를 화살표 case 레이블을 가진 switch문으로 작성하라.

public class Factorial {
   public static void main(String[] args) {
      System.out.println(factorial(5));
     }
     static int fatorial(int n) {
           //코드 작성
     }
}

 

<풀이>

package ch03;

public class P124N8 {

	public static void main(String[] args) {
		System.out.println(factorial(5));
	}
	
	static int factorial(int n) {
		//기존 swtich문으로 팩토리얼 프로그램 작성
		return switch(n) // 아래 case문을 실행한 값이 n에 저장되고 n값은 다시 factorial로 전송 
         {
            case 0 -> 1;
            case 1 -> 1;
            default -> factorial(n-1)*n;
		  } ;
	 }
}

 

📌 09 

다음은 foo() 메서드가 빠진 프로그램 일부와 실행결과이다. foo() 메서드를 완성하라. 

 

안녕 1
안녕하세요 1 2
잘 있어

  public static void main(String[] args) {
      foo("안녕",1);
      foo("안녕하세요", 1, 2);
     foo("잘 있어");
 }

 

<풀이>

package ch03;

public class P124N9 {

	public static void main(String[] args) {
		foo("안녕",1);
		foo("안녕하세요", 1, 2);
		foo("잘 있어");
	}
	public static void foo (String s, int a) {
        System.out.printf("%s %d\n", s, a);
    }
    public static void foo (String s, int a, int b) {
        System.out.printf("%s %d %d\n", s, a, b);
    }
    public static void foo (String s) {
        System.out.printf("%s", s);
    }
}

 

출력결과

📌 10

다음은 주어진 정수가 소수인지를 조사하는 프로그램의 일부이다. isPrime( ) 메서드를 완성하라. 여기서 소수는 1보다 크면서 1과 자신 외에는 나누지 않는 수이다. 

 

 

복사했습니다!