✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. CH4

if statement

  • 조건식의 다양한 예

조건문

조건식이 참일 조건

ch == 'y' || ch == 'Y'

문자 ch가 'y' 또는 'Y'일 때

ch == ' ' || ch == '\t' || ch == '\n'

문자 ch가 공백이거나 탭 또는 개행 문자일 때

'A' <= ch && ch <= 'Z'

문자 ch가 대문자일 때

'a' <= ch && ch <= 'z'

문자 ch가 소문자일 때

'0' <= ch && ch <= '9'

문자 ch가 숫자일 때

str.equals("yes")

문자열 str의 내용이 "yes"일 때(대소문자 구분)

str.equalsIgnoreCase("yes")

문자열 str의 내용이 "yes"일 때(대소문자 구분 안함)

  • else문 생략을 통한 코드 리팩토링 팁 : default 값을 지정해놓으면 else문을 굳이 쓰지 않아도 되고 코드가 훨씬 간결해진다.

import java.util.Scanner;

class Ex4_4 {
	public static void main(String[] args) { 
		int score  = 0;   
		char grade ='D'; 

		System.out.print("점수를 입력하세요.>");
		Scanner scanner = new Scanner(System.in);
		score = scanner.nextInt(); // 화면을 통해 입력받은 숫자를 저장한다.

		if (score >= 90) {         
			 grade = 'A';             
		} else if (score >=80) {    
			 grade = 'B'; 
		} else if (score >=70) {   
			 grade = 'C'; 
		} //else {                   
			 //grade = 'D'; 
		//}
		System.out.println("당신의 학점 "+ grade +"입니다");
	}
}

formatter %c를 이용하여 학점(A,B,C..)와 +-까지 출력하는 코드 + 리팩토

import java.util.Scanner;
//점수에 따라 자신의 점수와 학점(A,B,C) 와 +-까지 출력하는 코드 
class Ex4_5 {
	public static void main(String[] args) {
		int score = 0;
		char grade = 'C', opt = '0';
		
		System.out.println("점수를 입력해주세요.>");
		
		Scanner scanner = new Scanner(System.in);
		score = scanner.nextInt();
		
		System.out.printf("당신의 점수는 %d입니다%n",score);
		
		if(score >= 90) {
			grade = 'A';
			if(score >= 98) {
				opt = '+';
			} else if(score < 94) {
				opt = '-';
			}
		} else if (score >= 80){     
			grade = 'B';
			if (score >= 88) {
				opt = '+';
			} else if (score < 84)	{
				opt = '-';
			}
		} 
		System.out.printf("당신의 학점은 %c%c입니다%n",grade,opt);
	}
}

Previous조건문과 반복문Nextswitch

Last updated 4 years ago

Was this helpful?