본문 바로가기

IT일반과목/java

자바 배열 연습문제

package jongkyu.array;


import java.util.Random;

import java.util.Scanner;


public class Array {

Scanner sc = new Scanner(System.in);

Random r = new Random();

int c=0;

public void arrayTotal() {


while(true) {

System.out.println("\n배열 통합 문제");

System.out.println("1.정수형배열 odd[4]를 만든 후 1,3,5,7을 대입한 후 그대로 화면에 출력하라. ");

System.out.println("2.char 배열 Alpha[26]을 만들어 알파벳 대문자 A부터 Z까지 대입한후 화면에 출력");

System.out.println("3.이번에 char배열 Alpha[26]에 소문자 z부터 a까지 역순으로 대입한후 화면에 배열의 첫원소부터 출력");

System.out.println("0은 종료");

System.out.print("번호 : ");

int sel = sc.nextInt();



switch(sel) {

case 1 : array(); break;

case 2 : array2(); break;

case 3 : array3(); break;

case 0 : return ;


}

}



}






public void array() {


System.out.println("정수형배열 odd[4]를 만든 후 1,3,5,7을 대입한 후 그대로 화면에 출력하라.");

int [] odd = new int[4];


for(int i=0; i<4; i++) {

System.out.print("숫자 대입 : ");

odd[i] = sc.nextInt();

}

for(int i=0; i<odd.length; i++) {

System.out.print(odd[i] + "\t");

}

}

public void array2() {

char [] Alpha = new char[26];


for(int i=0; i<26; i++) {

System.out.print(Alpha[i]);

}

for(int i=0,cnt=65; i<26; i++, cnt++) {

//System.out.print("입력 :");

Alpha[i] = (char)cnt;

//System.out.print("\n");


}

for(int i=0; i<26; i++) {

System.out.print(Alpha[i]);

}


}

public void array3() {

char [] Alpha = new char[26];


for(int i=0; i<26; i++) {

System.out.print(Alpha[i]);

}

for(int i=0,cnt=97; i<26; i++, cnt++) {

//System.out.print("입력 :");

Alpha[i] = (char)cnt;

//System.out.print("\n");


}

for(int i=0; i<26; i++) {

System.out.print(Alpha[i]);

}


}



}