import java.util.Scanner;
public class Exam_02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*
System.out.print("1번째 수 : ");
int num1= Integer.parseInt(sc.nextLine());
System.out.print("2번째 수 : ");
int num2= Integer.parseInt(sc.nextLine());
System.out.print("3번째 수 : ");
int num3= Integer.parseInt(sc.nextLine());
System.out.println("num1 : " + num1);
System.out.println("num2 : " + num2);
System.out.println("num3 : " + num3);
//입출력하는 코드를 배열을 이용하면 코드를 줄일 수 있다.
*/
System.out.println("숫자가 몇개?");
int count=Integer.parseInt(sc.nextLine());
int[] num = new int[count];
//인덱스에 결과적으로 숫자가 들어가면 되는 것을 이용한 동적인 입력 가능
//int[] num = new int[3];
for(int i=0;i<num.length;i++) {
System.out.print((i+1)+"번째 수 :");
//num1,num2로 각각 변수로 만들었을때 i를 numi로 적용이 불가능하지만,(그냥 numi라는 변수를 얘기하는게 되어버림)
//배열에서는 index에 결과적으로 숫자만 들어가면 되므로 num[i]가 가능
num[i]=Integer.parseInt(sc.nextLine());
}
for(int i=0;i<num.length;i++) {
System.out.println(num[i]);
}
}
}
'자바 프로그래밍 > 코드' 카테고리의 다른 글
중복값 없이 랜덤 출력하기 - 카드섞기 기법 (0) | 2020.02.12 |
---|---|
배열과 스왑(swap)기법 (0) | 2020.02.12 |
배열참조변수 (0) | 2020.02.12 |
Baskin Robbins 31 Game (0) | 2020.02.11 |
경마 배팅 게임 (1) | 2020.02.11 |
Comment