Develop/JAVA

JAVA 기초 반복문(for,while,do~while)

roalwh 2023. 10. 16. 22:07
import java.util.Scanner;

public class Mainclass {

	public static void main(String[] args) {
		
		//for문
		System.out.print("input number : ");
		Scanner scanner = new Scanner(System.in);
		int inputNum =  scanner.nextInt();
		scanner.close();
		
		for (int i = 1; i < 10; i++) {
			System.out.printf("%d * %d = %d\n ", inputNum, i, (inputNum*i));
		}
		
		//while문
		System.out.print("Input number : ");
		int num = scanner.nextInt();
		int i = 1;
		while (i < 10) {
			System.out.printf("%d * %d = %d\n",  num, i, (num * i));
			i++;
		}
		
		//do ~while문
		do {
			System.out.println("무조건 1번은 실행합니다. ");
		} while (false);
				
		/*
		 * input number : 10
		 * 10 * 1 = 10
		 *  10 * 2 = 20
 		 * 10 * 3 = 30
 		 * 10 * 4 = 40
 		 * 10 * 5 = 50
		 *  10 * 6 = 60
		 *  10 * 7 = 70
		 *  10 * 8 = 80
		 *  10 * 9 = 90
		 *  Input number : 20
		 * 20 * 1 = 20
		 * 20 * 2 = 40
		 * 20 * 3 = 60
		 * 20 * 4 = 80
		 * 20 * 5 = 100
		 * 20 * 6 = 120
		 * 20 * 7 = 140
		 * 20 * 8 = 160
		 * 20 * 9 = 180
		 * 무조건 1번은 실행합니다. 
		 */
	}
}

'Develop > JAVA' 카테고리의 다른 글

JAVA 상속 특징  (0) 2023.10.17
JAVA 상속  (0) 2023.10.17
JAVA 조건문 if문, switch문  (0) 2021.10.19
JAVA 배열과 메모리  (0) 2021.10.18
JAVA 배열, Scanner  (0) 2021.10.14