본문 바로가기
  • developer
  • challenge
Java 예제

자바 입력 받아서 구구단 만들기

by 빵승 2024. 1. 5.

첫번째 방법

import java.util.Scanner;

 

public class ScanEx2 {

 

public static void main(String[] args) {

 

Scanner scan = new Scanner(System.in);

System.out.print("단수 입력 : ");

int dan= scan.nextInt();

 

System.out.println();

System.out.println(dan + " * " + 1 + "="+ dan*1);

System.out.println(dan + " * " + 2 + "="+ dan*2);

System.out.println(dan + " * " + 3 + "="+ dan*3);

System.out.println(dan + " * " + 4 + "="+ dan*4);

System.out.println(dan + " * " + 5 + "="+ dan*5);

System.out.println(dan + " * " + 6 + "="+ dan*6);

System.out.println(dan + " * " + 7 + "="+ dan*7);

System.out.println(dan + " * " + 8 + "="+ dan*8);

System.out.println(dan + " * " + 9 + "="+ dan*9);

단수 입력 : 5       (입력받은 결과값)

 

5 * 1=5

5 * 2=10

5 * 3=15

5 * 4=20

5 * 5=25

5 * 6=30

5 * 7=35

5 * 8=40

5 * 9=45

 

 

두번쨰 방법

 

import java.util.Scanner;

 

public class Main5 {

 

public static void main(String[] args) {

 

Scanner scan= new Scanner(System.in);

 

int dan;

 

do {

System.out.print("단수입력:");

dan= scan.nextInt();

} while(dan<2 || dan>9);

 

 

for(int i=1; i<10; i++) {

System.out.println(dan+"*"+i+"="+dan*i);

}

}

}

단수입력:7     (입력받은 결과값)

7*1=7

7*2=14

7*3=21

7*4=28

7*5=35

7*6=42

7*7=49

7*8=56

7*9=63