728x90
반응형
SMALL
  • 어떤 행동을 하는 것을 별도로 만들어놓은 것

main

initialize();

main_loop();

//자바는 클래스 기반으로 호출

 

-static

객체 생성을 하지 않고도 사용할 수 있는 keyword!!

 

-return type

돌려주는 값이 없다면 void !

돌려주는 값이 있으면 int, float, double, String, String[]

어떤 타입을 돌려줄 것인지 지정을 해야한다.

수업에서 매번 쓰는 main이 void type

//자바는 'public static void main(String[]' 이 고정!

 

-함수명

동사 + 대문자로 시작하는 명사

printScreen, getMemberVariables

 

-매개 변수(parameter)

함수가 외부로부터 값을 받기 위해 사용

입력값의 개수가 정해져 있을 때는 (int a, int b) 등으로 전체를 기술

입력값의 개수가 정해져 있지 않을 때 (String[] args)

 

-리턴값

return 값 // 돌려주는 값인 double 과 같음

void도 return을 사용할 수 있으며, 중단하고 아래 부분을 실행하지 않음

 

package Day04;

public class MinProject {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	String[] str = {"a","b","c"};
	int a = getArrayLength(str);
	System.out.println("str element 개수는 " + a);
}

public static int getArrayLength(String[] str) {
	int result = 0;
	result = str.length;
	return result;

}

}

//배열의 길이를 돌려주라

//결과값 str elemet 개수는 3

	// 1-0. 게임시작을 알려준다.

	// 1-1. 보물상자를 발견했다는 메시지를 출력하고 아무키 + 엔터를 기다린다. 색상은 옐로우

	// 1-2. 보물상자에서 랜덤으로 1개의 무기를 획득한다.
	// 		각 무기는 무기이름, 최소데미지, 최대데미지를 가짐

	String[] weapon_name = { "휴지", "목검", "대검", "대포", "에픽밸붕검"};
	int[] weapon_min_dam = {1, 3, 5, 0, 50};
	int[] weapon_max_dam = {3, 5, 10, 50, 100};

package Day04;

import Myutil.Colors;
import java.util.Random;
import java.util.Scanner;

/*

- 보물상자에서 무기를 5종류 중 하나 랜덤으로 획득한다.
- 길을 가다가 늑대, 산적, 드래곤 중 하나를 만난다.
- 무기를 가지고 둘 중 하나의 에너지가 0이하가 될 때까지 싸운다. (가장어려운)
- 승리 또는 패배에 따라 메시지를 출력하다.
*/

public class MinProject {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	//		System.out.println("가나다라");
String[] str = {"a","b","c"};
int a = getArrayLength(str);
System.out.println("str element 개수는 " + a);

// 1-0. 게임시작을 알려준다.
	Colors.p("운명의 데스티니 게임 start");

	// 1-1. 보물상자를 발견했다는 메시지를 출력하고 아무키 + 엔터를 기다린다. 색상은 옐로우
	System.out.println("길을 가다가" + Colors.YELLOW + "[보물상자]" + Colors.END + "를 발견했다.");

	// 1-2. 보물상자에서 랜덤으로 1개의 무기를 획득한다.
	// 		각 무기는 무기이름, 최소데미지, 최대데미지를 가짐

	String[] weapon_name = { "휴지", "목검", "대검", "대포", "에픽밸붕검"};
	int[] weapon_min_dam = {1, 3, 5, 0, 50};
	int[] weapon_max_dam = {3, 5, 10, 50, 100};

	Random rd = new Random();
	int sel = rd.nextInt(0,5);
	String my_weapon = weapon_name[sel];
	int my_min_dam = weapon_min_dam[sel];
	int my_max_dam = weapon_max_dam[sel];
	System.out.println(my_weapon + "(" + my_min_dam + "-" + my_max_dam + ")" + "을 획득하였습니다.");

	//String str1 = "3";
	//int a1 = Integer.parseInt(str1);
	//파이썬에서는 이렇게 쓰임

	float critical_ratio = rd.nextFloat() * 100;
	System.out.println("Critical Ratio : " + critical_ratio + "%");

	// 2. 몬스터를 만난다.
	String[] mon_name = {"늑대", "산적", "드래곤"};
	int[] mon_min_dam = {1, 5, 1};
	int[] mon_max_dam = {3, 10, 100};

	Random rd1 = new Random();

	int sel1 = rd1.nextInt(0,3);

	String my_mon = mon_name[sel1];
	int my_mon_min_dam = mon_min_dam[sel1];
	int my_mon_max_dam = mon_max_dam[sel1];

	System.out.println("당신은 길을 가다가 "+ my_mon + "을 만났습니다.");
package Day04;

import Myutil.Colors;
import java.util.Random;
import java.util.Scanner;

/*

- 보물상자에서 무기를 5종류 중 하나 랜덤으로 획득한다.
- 길을 가다가 늑대, 산적, 드래곤 중 하나를 만난다.
- 무기를 가지고 둘 중 하나의 에너지가 0이하가 될 때까지 싸운다. (가장어려운)
- 승리 또는 패배에 따라 메시지를 출력하다.
*/

	public class MinProject {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	//		System.out.println("가나다라");
	String[] str = {"a","b","c"};
	int a = getArrayLength(str);
	System.out.println("str element 개수는 " + a);

	// 1-0. 게임시작을 알려준다.
	Colors.p("운명의 데스티니 게임 start");

	// 1-1. 보물상자를 발견했다는 메시지를 출력하고 아무키 + 엔터를 기다린다. 색상은 옐로우
	System.out.println("길을 가다가" + Colors.YELLOW + "[보물상자]" + Colors.END + "를 발견했다.");

	// 1-2. 보물상자에서 랜덤으로 1개의 무기를 획득한다.
	// 		각 무기는 무기이름, 최소데미지, 최대데미지를 가짐

	String[] weapon_name = { "휴지", "목검", "대검", "대포", "에픽밸붕검"};
	int[] weapon_min_dam = {1, 3, 5, 0, 50};
	int[] weapon_max_dam = {3, 5, 10, 50, 100};

	Random rd = new Random();
	int sel = rd.nextInt(0,5);
	String my_weapon = weapon_name[sel];
	int my_min_dam = weapon_min_dam[sel];
	int my_max_dam = weapon_max_dam[sel];
	System.out.println(my_weapon + "(" + my_min_dam + "-" + my_max_dam + ")" + "을 획득하였습니다.");

	//String str1 = "3";
	//int a1 = Integer.parseInt(str1);
	//파이썬에서는 이렇게 쓰임

	float critical_ratio = rd.nextFloat() * 100;
	System.out.println("Critical Ratio : " + critical_ratio + "%");

	// 2. 몬스터를 만난다.
	String[] mon_name = {"늑대", "산적", "드래곤"};
	int[] mon_min_dam = {1, 5, 1};
	int[] mon_max_dam = {3, 10, 100};

	Random rd1 = new Random();

	int sel1 = rd1.nextInt(0,3);

	String my_mon = mon_name[sel1];
	int my_mon_min_dam = mon_min_dam[sel1];
	int my_mon_max_dam = mon_max_dam[sel1];

	System.out.println("당신은 길을 가다가 "+ my_mon + "을 만났습니다.");

	// 3-1. 전투를 시작한다. 초기 양쪽의 에너지는 100이다.
	int my_energy = 100;
	int mon_energy = 100;

	// 3-2. 무한루프로 전투를 한다. 둘 중 하나의 에너지가 0이하가 되면 탈출
	//		유저는 1. 공격 또는 2. 회복을 선택한다.
	//		공격인 경우는 최소-최대 데미지로 공격, 회복은 0~30사이의 에너지가 회복된다.
	//		몬스터는 공격만 하며, 유저와 한 번씩 교대로 행동한다.
	int user_input;
	int damage;

	while(true) {
	// 유저입력

		//유저입력(무용서 버전)
		//Scanner sc = new Scanner(System.in);
		//System.out.println("행동을 입력하십시오(1.공격, 2.회복) :");
		//user_input = sc.nextInt();

		//유저입력(무용서 버전)
		while(true) {
			Scanner sc = new Scanner(System.in);
			System.out.println("행동을 입력하십시오(1.공격, 2.회복) :");
			user_input = sc.nextInt();
			if(user_input == 1 || user_input == 2) break;
		}

	// 공격
		if(user_input == 1) {
			damage = cal_damage(my_min_dam, my_max_dam, critical_ratio);
			mon_energy -= damage;
			if(mon_energy < 0 ) mon_energy = 0;
			// 데미지를 입혔다는 메시지와 남은 에너지를 출력한다.
			System.out.println("당신의 공격으로 " + my_mon + "에게 " + damage + "의 피해를 입혔습니다.");
			System.out.println("당신의 에너지 " + my_energy + " ");

			//몬스터의 에너지가 0이면 탈출한다.
			if(mon_energy == 0 ) break;
		}

	// 회복
		else if(user_input == 2){

		}
	//몬스터 공격

	}

}

public static int getArrayLength(String[] str) {
	int result = 0;
	result = str.length;
	return result;

}

public static int cal_damage(int min_dam, int max_dam, float cri_ratio) {
	// min - max 사이의 데미지를 랜덤하게 획득
	Random rd = new Random();
	int base_dam = rd.nextInt(min_dam, max_dam +1);
	int final_dam = base_dam;
	// 크리티컬율에 따라 1.5배 적용 여부 결정

	if(rd.nextFloat() * 100 <= cri_ratio ) {
		final_dam *= 1.5;
	}

	return final_dam;

}

}

 

문제!!

에너지칸 만들기

유저 20칸 한칸 띄고 (for문)몬스터 20칸

ex) 유저 10칸 빈칸 11칸 몬스터 n칸

에너지 >30초

빨간색

 

package Day04;

import Myutil.Colors;
import java.util.Random;
import java.util.Scanner;

/*

- 보물상자에서 무기를 5종류 중 하나 랜덤으로 획득한다.
- 길을 가다가 늑대, 산적, 드래곤 중 하나를 만난다.
- 무기를 가지고 둘 중 하나의 에너지가 0이하가 될 때까지 싸운다. (가장어려운)
- 승리 또는 패배에 따라 메시지를 출력하다.
*/

public class MinProject {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub	

	//		System.out.println("가나다라");
	String[] str = {"a","b","c"};
	int a = getArrayLength(str);
	System.out.println("str element 개수는 " + a);	

	// 1-0. 게임시작을 알려준다.
	Colors.p("운명의 데스티니 게임 start");

	// 1-1. 보물상자를 발견했다는 메시지를 출력하고 아무키 + 엔터를 기다린다. 색상은 옐로우
	System.out.println("길을 가다가" + Colors.YELLOW + "[보물상자]" + Colors.END + "를 발견했다.");

	// 1-2. 보물상자에서 랜덤으로 1개의 무기를 획득한다.
	// 		각 무기는 무기이름, 최소데미지, 최대데미지를 가짐

	String[] weapon_name = { "휴지", "목검", "대검", "대포", "에픽밸붕검"};
	int[] weapon_min_dam = {1, 3, 5, 0, 50};
	int[] weapon_max_dam = {3, 5, 10, 50, 100};

	Random rd = new Random();
	int sel = rd.nextInt(0,5); //아무상자도 선택하지 않을 수 잇고, 최대 5개까지 선택가능하기에 0,5입력!
	String my_weapon = weapon_name[sel];
	int my_min_dam = weapon_min_dam[sel];
	int my_max_dam = weapon_max_dam[sel];
	System.out.println(my_weapon + "(" + my_min_dam + "-" + my_max_dam + ")" + "을 획득하였습니다.");

	//String str1 = "3";
	//int a1 = Integer.parseInt(str1);
	//파이썬에서는 이렇게 쓰임

	float critical_ratio = rd.nextFloat() * 100;
	System.out.println("Critical Ratio : " + critical_ratio + "%");

	// 2. 몬스터를 만난다.
	String[] mon_name = {"늑대", "산적", "드래곤"};
	int[] mon_min_dam = {1, 5, 1};
	int[] mon_max_dam = {3, 10, 100};

	Random rd1 = new Random();

	int sel1 = rd1.nextInt(0,3);

	String my_mon = mon_name[sel1];
	int my_mon_min_dam = mon_min_dam[sel1];
	int my_mon_max_dam = mon_max_dam[sel1];

	System.out.println("당신은 길을 가다가 "+ my_mon + "을 만났습니다.");

	// 3-1. 전투를 시작한다. 초기 양쪽의 에너지는 100이다.
	int my_energy = 100;
	int mon_energy = 100;

	// 3-2. 무한루프로 전투를 한다. 둘 중 하나의 에너지가 0이하가 되면 탈출
	//		유저는 1. 공격 또는 2. 회복을 선택한다.
	//		공격인 경우는 최소-최대 데미지로 공격, 회복은 0~30사이의 에너지가 회복된다.
	//		몬스터는 공격만 하며, 유저와 한 번씩 교대로 행동한다.
	int user_input;
	int damage;

	while(true) {
	// 유저입력

		//유저입력(무용서 버전)
		//Scanner sc = new Scanner(System.in);
		//System.out.println("행동을 입력하십시오(1.공격, 2.회복) :");
		//user_input = sc.nextInt();

		//유저입력(용서 버전)
		while(true) {
			Scanner sc = new Scanner(System.in);
			System.out.println("행동을 입력하십시오(1.공격, 2.회복) :");
			user_input = sc.nextInt();
			if(user_input == 1 || user_input == 2) break;
		}

	// 공격
		if(user_input == 1) {
			damage = cal_damage(my_min_dam, my_max_dam, critical_ratio);
			mon_energy -= damage;
			if(mon_energy < 0 ) mon_energy = 0;
			// 데미지를 입혔다는 메시지와 남은 에너지를 출력한다.
			System.out.println("당신의 공격으로 " + my_mon + "에게 " + damage + "의 피해를 입혔습니다.");
			System.out.println("당신의 에너지 " + my_energy + " ");

			printEnergy(my_energy, mon_energy);

			// 몬스터의 에너지가 0이면 탈출한다.
			if(mon_energy == 0 ) break;
		}

	// 회복
		else if(user_input == 2){
			int heal = rd.nextInt(0, 31);
			my_energy += heal;
			if(my_energy > 100) my_energy = 100;
			System.out.println(heal + "의 에너지가 회복되어" + my_energy + "가 되었습니다.");
			printEnergy(my_energy, mon_energy);
		}
	//몬스터 공격
		damage = cal_damage(my_mon_min_dam, my_mon_max_dam, 0);
		my_energy -= damage;
		if(my_energy <0) my_energy = 0;
		System.out.println(my_mon + "의 공격으로 " + damage + "의 피해를 입었습니다.");
		System.out.println("당신의 에너지 " + my_energy + "의 피해를 입었습니다." + my_mon
				+ "의 에너지" + mon_energy );

		printEnergy(my_energy, mon_energy);

		if(my_energy == 0) break;

	}

	printReslt(my_energy);
}

public static int getArrayLength(String[] str) {
	int result = 0;
	result = str.length;
	return result;

}

public static int cal_damage(int min_dam, int max_dam, float cri_ratio) {
	// min - max 사이의 데미지를 랜덤하게 획득
	Random rd = new Random();
	int base_dam = rd.nextInt(min_dam, max_dam +1);
	int final_dam = base_dam;
	// 크리티컬율에 따라 1.5배 적용 여부 결정

	if(rd.nextFloat() * 100 <= cri_ratio ) {
		final_dam *= 1.5;}

	return final_dam;
}

public static void printReslt(int my_energy) {
	if(my_energy == 0) { //패배
		System.out.println("몬스터가 말했습니다. \\\\"상대도 안되네 ㅋㅋㅋㅋ\\\\"");
		System.out.println("내 명품가방을 가져갔습니다.");

	}
	else { // 승리
		System.out.println("\\\\"당신 정말 강하군.\\\\" 몬스터가 말했습니다.");
		System.out.println("하지만 암흑대륙에는 나보다 강한 자가 2314명 더 있다.");
	}

}

	public static void printEnergy(int my_energy, int mon_energy) {
		if(my_energy > 30 ) {
			System.out.print(Colors.GREENBG); }
		else {
			System.out.print(Colors.REDBG);
		}

		for(int i=0; i<my_energy/5;i++) {
			System.out.print(" ");
		}
		System.out.print(Colors.END);

		for(int i=0;i<(21-my_energy/5);i++)
			System.out.print(" ");

		if(mon_energy > 30 ) {
				System.out.print(Colors.GREENBG);
		}
		else {
				System.out.print(Colors.REDBG);
		}
		for(int i=0; i<mon_energy/5;i++) {
			System.out.print(" ");
		}
		System.out.print(Colors.END);
		System.out.println();
	}

}

 

728x90
반응형
LIST

'개발 > JAVA' 카테고리의 다른 글

상속  (0) 2023.01.09
클래스 심화  (0) 2023.01.09
Colors (추후 수정 필요 !)  (0) 2023.01.09
배열 (Array)  (0) 2023.01.09
제어문(Control Statement)  (0) 2023.01.09
728x90
반응형
SMALL
  • Eclipse에서는 플러그인 필요

       install new software : https://www.mihai-nita.net/eclipse

 

My Eclipse Plugins & Java tools — Internationalization Cookbook

This is an Eclipse update page It is intended for Eclipse to install / update my plugins. There is no point in opening it in your browser. Sorry, yes, it was a real page before GitHub killed the “downloads” feature and I had to shuffle things around. T

www.mihai-nita.net

 

  • 많은 양의 데이터가 화면에 출력되며 중요 데이터를 강조하고 싶을 때
  • 내 프로그램은 내 자존심
  • 글자 색상을 지정하는 방법
  • 색을 바꾸고 싶은 구간 앞뒤에 이스케이프 시퀀스(Escape Sequence)를 따른 예약 문자를 붙어서 사용

//arj 압축 lha 압축풀기 zip

 

(1)기본 8색

'\033[색상코드m' + 문장 + '\033[0m'

색상코드 : 30~37 일반 8색, 90~97 밝은일반8색

40~47 배경-일반8색, 100~107 배경-밝은 일반 8색

 

(2)확장 256색

글자색 : '\033[38;5;색상코드m' + 문장 + '\033[0m'

배경색 : '\033[48;5;색상코드m' + 문장 + '\033[0m'

//구글에서 265color를 입력하면 색이 나옴

 

(3)True color(256X256X256) - RED, GREEN, BLUE

글자색 : '\033[38;2;색상코드m' + 문장 + '\033[0m'

배경색 : '\033[48;2;색상코드m' + 문장 + '\033[0m'

색상코드 : R;G;B 0xFF 255

 

 

package Myutil;

public class Colors {
public static String RED = "\\033[31m";
public static String GREEN = "\\033[32m";
public static String YELLOW = "\\033[33m";
public static String BLUE = "\\033[34m";
public static String MAGENTA = "\\033[35m";
public static String CYAN = "\\033[36m";
public static String WHITE = "\\033[37m";
public static String BLACK = "\\033[30m";

public static String REDBG = "\\\\033[101m";
public static String GREENBG = "\\\\033[102m";

public static String END = "\\\\033[0m";

public static void main(String[] args) {
//  TODO Auto-generated method stub
		System.out.println(RED + "RED" + END);
		//결과값 RED
		System.out.println(REDBG + "   " + END);
		//결과값 빨강색

}

}
package Day03;

import Myutil.Colors;

public class ColorTest {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	//Colors cl = new Colors();
	//원래라면 이렇게 써야하지만, static를 썼다면 객체를 이렇게 만들지 않아도 됨
	System.out.println(Colors.RED + "Color Test" + Colors.END);
	//결과값 Color Test
	//Colors cl = new Colors();
	//cl = 5; 자바에서는 에러가 나지만, 파이썬에서는 가능함

	//int color =100;

	// basic 8 color

	for(int color=100; color<108; color++) {
		System.out.print("\\\\033[" + color + "m" + "   " +Colors.END);
	}

}

}
package Day03;

import Myutil.Colors;

public class ColorTest {	

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	//Colors cl = new Colors();
	//원래라면 이렇게 써야하지만, static를 썼다면 객체를 이렇게 만들지 않아도 됨
	System.out.println(Colors.RED + "Color Test" + Colors.END);
	//결과값 Color Test
	//Colors cl = new Colors();
	//cl = 5; 자바에서는 에러가 나지만, 파이썬에서는 가능함

	//int color =100;

	// basic 8 color

	for(int color=100; color<108; color++) {
		System.out.print("\\\\033[" + color + "m" + "   " +Colors.END);
	}

	System.out.println();

	//extended 256 color

	for(int color=0; color<256; color++) {
		System.out.print("\\\\033[48;5;" + color +"m"+ " ");

	}

	System.out.println(Colors.END);

	// true Color
	String[] str = new String[4];
	for(int color=128; color<256; color++) {
		str[0] +="\\\\033[48;2;" + color + ";0;0" + "m" + " ";
	}

}

}
컬러테스트
package Day03;

import Myutil.Colors;
import java.util.Random;

public class ColorTest {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	//Colors cl = new Colors();
	//원래라면 이렇게 써야하지만, static를 썼다면 객체를 이렇게 만들지 않아도 됨
	System.out.println(Colors.RED + "Color Test" + Colors.END);
	//결과값 Color Test
	//Colors cl = new Colors();
	//cl = 5; 자바에서는 에러가 나지만, 파이썬에서는 가능함

	//int color =100;

	// basic 8 color

	for(int color=100; color<108; color++) {
		System.out.print("\\\\033[" + color + "m" + "   " +Colors.END);
	}

	System.out.println();

	//extended 256 color

	for(int color=0; color<256; color++) {
		System.out.print("\\\\033[48;5;" + color +"m"+ " ");

	}

	System.out.println(Colors.END);

	// true Color
	String[] str = {"","","",""};  //new String[4]; null이 붙어서 나옴
	for(int color=128; color<256; color++) {
		str[0] +="\\\\033[48;2;" + color + ";0;0" + "m" + " ";
		str[1] +="\\\\033[48;2;" + "0;" + color + ";0" + "m" + " ";
		str[2] +="\\\\033[48;2;" +  ";0;0"+ color + "m" + " ";
		str[3] +="\\\\033[48;2;" +  color + ";" + color + ";" + color + "m" + " ";
	}
	System.out.println(str[0]);
	System.out.println(str[1]);
	System.out.println(str[2]);
	System.out.println(str[3]);

}

public static void p(String title) {
	System.out.println("******************");
	Random rd = new Random();
	int color = rd.nextInt(0,256);
	System.out.print("\\\\033[38;5;" + color + "m" + title);
}

}

 

\\는 두개씩 인데 자꾸 4개로 불어나와요....

노션에서는 안그렇는데 추후 수정 필요!!

728x90
반응형
LIST

'개발 > JAVA' 카테고리의 다른 글

클래스 심화  (0) 2023.01.09
함수(function)  (0) 2023.01.09
배열 (Array)  (0) 2023.01.09
제어문(Control Statement)  (0) 2023.01.09
Random (임의의 수를 얻는 도구)  (0) 2023.01.09
728x90
반응형
SMALL
  • 동일한 특성을 갖는 변수의 모음
    • 0부터 시작(cf R, fortran, pascal에서는 1로 시작)
ex) 
student1
student2
student3
.......
student1000          //이렇게 하나하나 만드는 것이 아니라
 student[1000]  > student[38] = 'diogfoieh'
                      //대 괄호 안에 숫자를 넣어주면 알아서 만듬 //크기 x 떨어진 거리만큼 있겠다는 의미

 lista = [1, 3, 'jwlee', String str]

파이썬은 다 받아주지만, 자바와 C는 안됨! //참고사항
장점은 꺼낼때 어떤 타입인지 알 수 있다.
package Day03;

public class ArrayTest {	

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	System.out.println("**** Array Test ****");

	String[] student1 = new String[5];
	String[] student2 = {"A", "B", "C", "James", "E"};
	int[] student_score = new int[5]; //고정된 숫자는 중간에 바꾸지 못한다.

	for(int i=0; i<5; i++) {
		System.out.println(student2[i]);
	}

	System.out.println(student2.length);

	for(int i=0;i<student2.length;i++) {
		System.out.println(student2[i]);

	} //자주 나오는 문장

	for(String str : student2) {
		System.out.println(str);
	} //자주 나오지는 않지만 까먹을 수 있는 문장!

	}

}
package Day03;

public class ArrayTest {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	System.out.println("**** Array Test ****");

	String[] student1 = new String[5];
	String[] student2 = {"A", "B", "C", "James", "E"};
	int[] student_score = new int[5]; //고정된 숫자는 중간에 바꾸지 못한다.

	for(int i=0; i<5; i++) {
		System.out.println(student2[i]);
	}

	System.out.println(student2.length);

	for(int i=0;i<student2.length;i++) {
		System.out.println(student2[i]);

	}

	for(String str : student2) {
		System.out.println(str);
	}

	// 2-d array
	int[][] m_array = new int[3][4];
	int[][] m_array2 = {{1,2,3},{4,5,6}};
	int[][] m_array3 = {{1,2,3},{4,5,6}};

	// 5를 선택하는 방법
	System.out.println(m_array2[1][1]);

	System.out.println(m_array2[0]);
	m_array2[0][2] = 100;
	System.out.println(m_array2[0]);

	System.out.println(m_array2);
	System.out.println(m_array3);

	System.out.println(m_array2 == m_array3);

	//System.out.println(m_array2 == m_array3);
	//2와 3이 같은 값을 가지고 있더라도 완전히 같지 않다. 동명이인이랑 같은 의미

	//System.out.println(m_array[0][0] == m_array3[0][0]);
	//단 위에 문장은 같음!!

	}

}

3차원 이상 배열 [][][]

정신적으로 만들지 않음...

2차원까지 하는 이유는 여기까지가 효율적이기 때문

int[][][][][] m_array = new int[3][4][5][6][7];

 

package Day03;

public class ArrayTest {

	public static void main(String[] args) {
	//  TODO Auto-generated method stub
	System.out.println("**** Array Test ****");

	String[] student1 = new String[5];
	String[] student2 = {"A", "B", "C", "James", "E"};
	int[] student_score = new int[5]; //고정된 숫자는 중간에 바꾸지 못한다.

	for(int i=0; i<5; i++) {
		System.out.println(student2[i]);
	}

	System.out.println(student2.length);

	for(int i=0;i<student2.length;i++) {
		System.out.println(student2[i]);

	}

	for(String str : student2) {
		System.out.println(str);
	}

	// 2-d array
	int[][] m_array = new int[3][4];
	int[][] m_array2 = {{1,2,3},{4,5,6}};
	int[][] m_array3 = {{1,2,3},{4,5,6}};

	// 5를 선택하는 방법
	System.out.println(m_array2[1][1]);

	System.out.println(m_array2[0]);
	m_array2[0][2] = 100;
	System.out.println(m_array2[0]);

	System.out.println(m_array2);
	System.out.println(m_array3);

	System.out.println(m_array2 == m_array3);

	//System.out.println(m_array2 == m_array3);
	//2와 3이 같은 값을 가지고 있더라도 완전히 같지 않다. 동명이인이랑 같은 이미

	//System.out.println(m_array[0][0] == m_array3[0][0]);
	//단 위에 문장은 같음!!

	}

}
728x90
반응형
LIST

'개발 > JAVA' 카테고리의 다른 글

함수(function)  (0) 2023.01.09
Colors (추후 수정 필요 !)  (0) 2023.01.09
제어문(Control Statement)  (0) 2023.01.09
Random (임의의 수를 얻는 도구)  (0) 2023.01.09
User Input (사용자로부터 입력을 받는 기능)  (0) 2023.01.09

+ Recent posts