Java/JetBrain Project

JetBrain [Easy] Bulls and Cows - Stage 2

aliceintr 2020. 12. 11. 03:51
반응형

package bullscows;

import java.util.Scanner;

public class Main {


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

        int cows = 0;
        int bulls = 0;
        boolean flag = true;

        String userInput = sc.nextLine();
        char[] chars = userInput.toCharArray();

        int[] answers = {9,3,0,5};
        int[] nums = new int[chars.length];
        int option = 0;
        for (int i =0; i < chars.length;i++){
            nums[i] = Character.getNumericValue(chars[i]);
            if (nums[i] == 9 || nums[i] == 3 || nums[i] == 0 || nums[i] == 5) {
                if (nums[i] == answers[i]) {
                    bulls++;
                } else {
                    cows++;
                }
            }
        }


        if(bulls == 0 && cows == 0){
            System.out.println("Grade: None. The secret code is 9305.");
        }else {
            if (bulls == 0) {
                System.out.println("Grade: " + cows + " cow(s). The secret code is 9305.");
            } else if (cows == 0) {
                System.out.println("Grade: " + bulls + " bull(s). The secret code is 9305.");
            } else {
                System.out.println("Grade: " + bulls + " bull(s) and " + cows + " cow(s). The secret code is 9305.");
            }

        }
    }
}
반응형