[Java1000제] Simple Quiz 2 - 간단한 다지선다형 문제

유콩·2021년 12월 7일
0

문제

[문제2] 문제1에서 완성된 코드에 사용자 입력을 받아서 정답여부를 판단하여 마지막에 최종점수를 실행결과와 같이 출력하도록 코드를 완성하세요.

[예제QuizEx2.java]

import java.util.*;

class QuizEx2 {
      public static void main(String[] args) {
            String[] data = {
                  "다음 중 키워드가 아닌 것은?`2`final`True`if`public",
                  "다음 중 자바의 연산자가 아닌 것은?`6`&`|`++`!=`/`^",
                  "다음 중 메서드의 반환값이 없음을 의미하는 키워드는?`1`void`null`false`",
            };
           
            Scanner s = new Scanner(System.in);
            int score = 0;

            for(int i=0;i < data.length;i++) {
                  String[] tmp = data[i].split("`",3);

                  String question = tmp[0];
                  String answer = tmp[1];
                  String[] choices = tmp[2].split("`");

                  System.out.println("["+(i+1)+"] "+question);
                 
                  for(int x=0;x < choices.length;x++) {
                        System.out.print((x+1)+"."+choices[x]+"\t");
                  }
                  System.out.println(); 


                  /*

                       알맞은 코드를 넣으세요.

                  */

 

                  System.out.println();
                  System.out.println();
            } 
           

            // 알맞은 코드를 넣으세요.

      } // main
}

[실행결과]

[1] 다음 중 키워드가 아닌 것은?
1.final 2.True 3.if 4.public
[답]2

[2] 다음 중 자바의 연산자가 아닌 것은?
1.& 2.| 3.++ 4.!= 5./ 6.^
[답]6

[3] 다음 중 메서드의 반환값이 없음을 의미하는 키워드는?
1.void 2.null 3.false
[답]1

정답개수/전체문항수 :3/3

https://cafe.naver.com/javachobostudy/24681

나의 풀이

문제에서 주어진대로 맞췄을 경우 score을 +1 시켜 게임이 종료되면 총 몇개를 맞췄는지 출력한다.

import java.util.*;

class QuizEx2 {
    public static void main(String[] args) {
        final String RESULT = "정답개수/전체문항수 : ";

        String[] data = {
            "다음 중 키워드가 아닌 것은?`2`final`True`if`public",
            "다음 중 자바의 연산자가 아닌 것은?`6`&`|`++`!=`/`^",
            "다음 중 메서드의 반환값이 없음을 의미하는 키워드는?`1`void`null`false`",
        };

        Scanner s = new Scanner(System.in);
        int score = 0;
        String input = "";

        for (int i = 0; i < data.length; i++) {
            String[] tmp = data[i].split("`",3);

            String question = tmp[0];
            String answer = tmp[1];
            String[] choices = tmp[2].split("`");

            System.out.println("[" + (i + 1) + "] " + question);

            for (int x = 0; x < choices.length; x++) {
                System.out.print((x + 1) + "." + choices[x] + "\t");
            }
            System.out.println();

            input = s.nextLine();
            if (input.equals(answer)) {
                score++;
            }

            System.out.println();
            System.out.println();
        }

        System.out.println(RESULT + score + "/" + data.length);

    }
}

저자 풀이

심플퀴즈3번 문제를 가져왔다. 작성하는 코드가 짧아 매우 동일하다. 😁😁

import java.util.*;

class QuizEx3 {
      public static void main(String[] args) {
            String[] data = {
                  "다음 중 키워드가 아닌 것은?`2`final`True`if`public",
                  "다음 중 자바의 연산자가 아닌 것은?`6`&`|`++`!=`/`^",
                  "다음 중 메서드의 반환값이 없음을 의미하는 키워드는?`1`void`null`false`",
            };
           
            Scanner s = new Scanner(System.in);
            int score = 0;

            shuffle(data); // 문제를 섞는다.

            for(int i=0;i < data.length;i++) {
                  String[] tmp = data[i].split("`",3);

                  String question = tmp[0];
                  String answer = tmp[1];
                  String[] choices = tmp[2].split("`");

                  answer = choices[Integer.parseInt(answer)-1];

                  shuffle(choices); // 선택지를 섞는다.

                  System.out.println("["+(i+1)+"] "+question);

                  for(int x=0;x < choices.length;x++) { 
                      /*

                               코드를 완성하세요.

                      */

                  }

                  System.out.println();
                  System.out.print("[답]");
                  String input = s.nextLine();
                 
                  if(input.equals(answer)) {
                        score++;
                  }

                  System.out.println();
                  System.out.println();
            }

            System.out.println("정답개수/전체문항수 :"+score+"/"+data.length);
      } // main

      public static void shuffle(String[] data) {

           // 코드를 완성하세요.

           //  1. 배열data의 개수가 0보다 같거나 적으면 메서드 전체를 빠져나간다.

           //  2. 선택지의 순서를 뒤바꾼다. 반복문과 Math.random()사용
      } // shuffle()
}

0개의 댓글

관련 채용 정보