Jam's story

8일차 본문

Java

8일차

애플쩀 2022. 2. 24. 16:45

내가 푼 코드 

	char grade;
		String sGrade, regex;
		Scanner sc=new Scanner(System.in);
		do {
			System.out.println("수,우,미,양,가");
			sGrade=sc.next();
			regex="[수,우,미.양,가]";
		} while (! sGrade.matches(regex));
	
		grade=sGrade.charAt(0);
		System.out.println(grade);

강사님 코드 

		char grade;
		String sGrade, regex;
		Scanner sc=new Scanner(System.in);
		do {
			System.out.println("수,우,미,양,가");
			sGrade=sc.next();
			regex="[수우미양가]";
		} while (! sGrade.matches(regex));
	
		grade=sGrade.charAt(0);
		System.out.println(grade);

내 코드링 다른점 

  • String regex=[수우미양가] 쉼표없이 한번에 씀

	char grade;
		String sGrade = null, regex;
		int check=0;
		Scanner sc=new Scanner(System.in);
		do {
			if(check>=1) System.out.println(check+"번 입력 잘못함");
			if(check==5) {System.out.println("프로그램종료"); break;}
			System.out.println("수,우,미,양,가");
			sGrade=sc.next();
		 	regex="[수우미양가]";
			check++;	
		} while (! sGrade.matches(regex) && check <5);
		grade=sGrade.charAt(0);
		System.out.println(grade);
  • 아무것도 입력받지 않았을때, sGrade가 초기화가 안되어있기 때문에 오류가 나온다 
  • String sGrade=null; 로 초기화를 해준다 .
main 함수 

return값을 주면 메인 메소드를 빠져나갈 수 있고, 프로그램을 종료할 수 있다,

System.exit(정수 값)

프로세스 종료 시킴

 

즉 .3가지 방법이  있다.  [ 5번 틀린경우에 빈복문 종료] 

1. break; (제어문 종료)

2. return; (main 종료)

3.System.exit(정수값) -프로그램종료  

 

 

regex

Pattern (Java Platform SE 8 ) (oracle.com) 정규표현식 형식 사이트 

  • ^ 는 부정의 의미 
  • ? 한자리 와도 3좋고 아예 안와도 좋다 

 

국어점수를 유효성 검사 
regex="\\d{1,2}";
regex="\\d{1,2} | \\d{3}"; =regex="\\d{1,3}"; //숫자 한자리~3자리 가능 but 111,999도 가능해짐
regex="\\d(1,2)| 100";  //09도 가능한게 문제
regex="[1-9]?\\d|100"; //이게 정답 0~100 
[1-9]?   이것은 한자리 있어도 되고 없어도 된다. d 는 모든 숫자 , | 는 그리고  100이어도 된다 .
과제

메서드

오버로딩과 오버라이딩의 다른점 

오버로딩: 같은 메소드 이름으로 중복 선언된것이 중복함수, 영어로는 오버로딩

매개변수나 타입이 달라야한다. 

 

 

내가쓴코드

public static void main(String[] args) {
      // TODO Auto-generated method stub
      int score=korScore();
      System.out.printf("국어 점수는 %d",score);
   }
   public static int korScore() {
      Scanner sc=new Scanner(System.in);
      System.out.println("국어점수 입력:");
      String skor=sc.next();
      int kor = 0;
      String regex="[1-9]?\\d|100";
      if(skor.matches(regex))  kor=Integer.parseInt(skor);
      return kor;
   }

강사님 코드

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
		int kor=getScore("국어");
		System.out.println(kor);
		int eng=getScore("영어");
		System.out.println(eng);
	}

public static int getScore(String subject,Scanner sc) {
		
		int iScore= 0;
		String score;
		String regex="[1-9]?\\d|100";
		do {
			System.out.println(subject+"점수 입력:");
			   score=sc.next();	
		} while (!score.matches(regex));
		 iScore =Integer.parseInt(score);
		return iScore;
	}

내 코드랑 다른점 

  • do-while문을 써서 계속 입력받게함
  • 국어 점수뿐만 아니라 다른 점수도 받을 수 있게 해서 재사용성을 높힘 
  • 매개변수에 스캐너를 넣어서 자원낭비를 줄임 성능이 올라감 public static int getScore(String subject,Scanner sc
햠수 호출하는 방법 
  • call by Name ->함수 이름으로 호출 
  • call by Value -> 실제 값을 넘겨서 호출 
  • call by Point (자바에서는 사용 X)
  • call by reference  ->참조형  (배열 클래스, 인터페이스)를 가지고 메소드 호출 방법 

swap() 함수를 이용해서 값 바꾸기 

- 호출을 한 후에 선언하는 것이 더 편하다 

 

재귀함수 

함수 안에서 자기 자신 함수를 호출한다. 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int result=sum(10);
		System.out.println(result);
		int result2=recursiveSum(10);
		System.out.println(result);
		
	}//main
	private static int recursiveSum(int n) {
		// TODO Auto-generated method stub
		if(n==1) return n;
		else return n+recursiveSum(n-1);
	}
	/**
	 * @param i
	 * @return
	 */
	private static int sum(int n) {
		// TODO Auto-generated method stub
		int sum=0;
		for(int i=0; i<=n; i++) {
			sum+=i;
		}
		return sum;
	}

 

'Java' 카테고리의 다른 글

10일차  (0) 2022.03.02
9일차  (0) 2022.02.25
7일차  (0) 2022.02.23
5일차  (0) 2022.02.21
4일차  (0) 2022.02.18
Comments