✨
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

while statement

  • 반복문 선택 기준

반복횟수를 알 때 : for문

반복횟수를 모를 때 : while문

while(i-- != 0) {
    System.out.println(i+"I can do it!");
}

//아래와 같다.
while(i != 0) {
    i--;
    System.out.println(i+"I can do it!");
}

각 자릿수의 합을 구하는 코드

import java.util.*;

class digitsum {
	public static void main(String[] args) {
		int num = 0, digitsum = 0;
		System.out.print("숫자를 입력하세요.(예:12345)>");
		
		Scanner scanner = new Scanner(System.in);
		String tmp = scanner.nextLine();
		num = Integer.parseInt(tmp);//tmp를 숫자 정수로 변환!
		
		while(num!=0) {//num=12345->1234->123->12->1->0이되면서 while문 탈출 
			digitsum += num%10;//sum=5->5+4->5+4+3->5+4+3+2->5+4+3+2+1
			System.out.printf("sum=%d num=%d%n",digitsum,num);
			
			num /= 10;//num=1234->123->12->1->0
		}
		System.out.printf("각 자리수의 합:%3d",digitsum);
	}
}
  • do-while문

: 블럭{}을 최소한 한 번 이상 반복한다.=>사용자 입력을 받을 때 유용하다. 단, while문 끝에 ';' 잊지 않도록 주의한다!

do {

} while();
  • do-while문 연습 : 숫자 맞추기

//사용자에게 숫자를 입력받아서 숫자 맞추는 코드
import java.util.*;

class Ex4_15 {
	public static void main(String[] args) {
		int input = 0, answer = 0;
		
		answer = (int)(Math.random()*100) +1;
		System.out.println("anser="+answer);
		Scanner scanner = new Scanner(System.in);
		
		do {
			System.out.print("1부터 100사이의 정수를 입력하세요.>");
			input = scanner.nextInt();
			
			if(input > answer) {
				System.out.println("더 작은 수를 입력하세요 ");	
			} else if(input < answer) {
				System.out.println("더 큰 수를 입력하세요");			
			}
			
			
		} while(answer!= input);
		
		System.out.println("정답입니다! ");
	}
}

while 문으로 바꿨을 경우 입력을 받는 코드가 2번이상 중복되기 때문에 좋지 않다.

Previousfor statementNextbreak, continue, named iterated statement

Last updated 4 years ago

Was this helpful?