[Java] tip: 구구단 프로그램 만들기 (Scanner, while문)

febCho·2023년 10월 8일
0

Java

목록 보기
21/53
package kr.s05.operation;

public class WhileMain03 {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		
		System.out.print("단 입력 : ");
		int dan = input.nextInt();
		
		System.out.println(dan + "단");
		System.out.println("===============");
		
		int i=1;
		while(i <=9) {
			System.out.println(dan + "*" + i + "=" + dan*i);
			i++;
		}
		System.out.println("구구단 외우기 끝");
		
		input.close();
	}
}
  1. for문과 달리, 초기값과 조건식, 증감식이 어떻게 작성되어 있는지 비교하면 while문의 특성을 이해하는데 도움이 된다.
    Scanner, for문을 활용해 구구단 프로그램 만들기

1) 초기값: int i=1;
2) 조건식: i <=9
곱해지는 수인 i가 1에서 9까지 while문 안에서 루프를 돈다.
3) 증감식: i++;
i 값에 변동이 없어 무한 루프에 빠지지 않도록 곱하고 나면 i를 하나 증가시켜 준다.

  1. 1~9까지 곱하고 나면 System.out.println("구구단 외우기 끝");를 출력하고 input.close(); 해준다.
profile
Done is better than perfect.

0개의 댓글