[면접예상] 20230623

neul·2023년 6월 23일
0

면접예상

목록 보기
12/36
post-thumbnail

🌵아래를 프로그래밍 하시오.

이름:kom
국어:80
영어:90
수학:100
kom님의 평균은90.0성적은수입니다.
계속?y
이름:yun
국어:90
영어:70
수학:55
yun님의 평균은71.66666666666667성적은미입니다.
계속?yes
이름:한글
국어:90
영어:75
수학:80
한글님의 평균은81.66666666666667성적은우입니다.
계속?klsjdaf
종료되었습니다.

종료되었습니다.

class Grade2 {
		String name;
	   int kor, eng, math; //인스턴트 변수

	   void setGrade(int kor, int eng, int math) { 
	      this.kor = kor;
	      this.eng = eng;
	      this.math = math; 
	   }

	   double getAvg() { 
	      return (kor + eng + math) / 3.0;
	   }

	   char getGrade() {
	      char ch = '가';
	      double avg = getAvg(); 
	      
	      if(avg >=90) {
	         ch='수';
	      }
	      else if (avg >=80) {
	         ch='우';
	      }
	      else if (avg >=70) {
	         ch='미';
	      }
	      else if (avg >=60) {
	         ch='양';
	      }
	      
	      return ch;
	   }
	}
public class Java_02_console {

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

	        boolean continueProgram1 = true;

	        while (continueProgram1) {
	            System.out.print("이름: ");
	            String name = scanner.nextLine();

	            System.out.print("국어 성적: ");
	            int kor = scanner.nextInt();

	            System.out.print("영어 성적: ");
	            int eng = scanner.nextInt();

	            System.out.print("수학 성적: ");
	            int math = scanner.nextInt();

	            // 개행 문자(\n) 제거
	            scanner.nextLine();

	            Grade2 grade = new Grade2();
	            grade.setGrade(kor, eng, math);

	            System.out.print(name + "님의");
	            System.out.print("평균은 : " + grade.getAvg() + "이고 ");
	            System.out.println("성적은 " + grade.getGrade() + "입니다.");

	            System.out.print("계속하시겠습니까? (Yes/No): ");
	            String continueChoice = scanner.nextLine();

	            if (continueChoice.equalsIgnoreCase("No")) {
	                continueProgram1 = false;
	            }
	        }

🌵아래의 String 함수에 대하여 예를들어 설명하시오.

equals()
: 두 문자열이 내용적으로 동일한지 확인

String str1 = "안녕";
String str2 = "안녕";
boolean isEqual = str1.equals(str2); // true

indexOf()
: 특정 부분 문자열이 문자열 내에서 첫 번째로 나타나는 위치의 인덱스를 반환

String str = "안녕하세요";
int index = str.indexOf("하"); // 2

length()
: 문자열의 길이(문자 수)를 반환

String str = "안녕하세요";
int length = str.length(); // 5

substring()
: 지정된 시작 인덱스와 끝 인덱스를 기반으로 문자열의 일부분을 추출

String str = "안녕하세요";
String sub = str.substring(2, 5); // "하세요"

toUpperCase()와 toLowerCase()
: 문자열을 대문자 또는 소문자로 변환

String str = "Hello";
String upper = str.toUpperCase(); // "HELLO"
String lower = str.toLowerCase(); // "hello"

concat()
: 두 개의 문자열을 연결(결합)

String str1 = "안녕";
String str2 = "하세요";
String result = str1.concat(str2); // "안녕하세요"

startWith()
: 문자열이 지정된 접두사로 시작하는지 확인

String str = "안녕하세요";
boolean startsWithAnnyeong = str.startsWith("안녕"); // true

replace
: 문자열에서 특정 문자 또는 부분 문자열의 모든 발생을 다른 문자 또는 부분 문자열로 대체

String str = "안녕하세요";
String replaced = str.replace("하세요", "반갑습니다"); // "안녕반갑습니다"

trim()
: 문자열의 시작과 끝에 있는 공백 문자를 제거

String str = "   안녕하세요   ";
String trimmed = str.trim(); // "안녕하세요"

contains
: 문자열이 특정 부분 문자열을 포함하는지 확인

String str = "

🌵아래를 프로그래밍 하시오.

======================
힌트) length 함수와 charAt 함수사용
영어 단어를 입력하세요.
dakjfivnlwe
총 글자 수는: 11개 입니다.
모음은 : 3개 입니다.
자음은 : 8개 입니다.

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

        System.out.print("영어 단어를 입력하세요: ");
        String word = scanner.nextLine();

        int length = word.length();
        int vowelCount = 0;
        int consonantCount = 0;

        for (int i = 0; i < length; i++) {
            char ch = Character.toLowerCase(word.charAt(i));
            if (isVowel(ch)) {
                vowelCount++;
            } else if (Character.isLetter(ch)) {
                consonantCount++;
            }
        }

        System.out.println("총 글자 수는: " + length + "개 입니다.");
        System.out.println("모음은: " + vowelCount + "개 입니다.");
        System.out.println("자음은: " + consonantCount + "개 입니다.");

        scanner.close();
    }

    private static boolean isVowel(char ch) {
        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
    }
}
  • 위의 코드에서는 입력받은 영어 단어의 글자 수, 모음의 개수, 자음의 개수를 계산하여 출력한다.
    • length() 함수를 사용하여 문자열의 길이를 구하고, charAt() 함수를 사용하여 각 문자에 접근한다
    • isVowel() 함수를 사용하여 모음인지 확인
    • 대소문자를 구분하지 않고 모음과 자음을 계산하므로, 입력된 문자를 소문자로 변환하여 비교함

🌵가위바위보 프로그램을 짜시오.

힌트 - random 함수 사용
========================
가위, 바위, 보 중 하나를 입력하세요.
가위 // 유저가 입력
바위 // 
졌습니다.
계속하시겠습니까?(Y/N)
Y
가위, 바위, 보 중 하나를 입력하세요.
바위
가위
이겼습니다.
계속하시겠습니까?(Y/N)
N
프로그램을 종료합니다.
profile
🍙

0개의 댓글

관련 채용 정보