run as -> run configuration -> browse에서 현재 파일 선택 -> arguments에서 출력하고 싶은 문구 입력후 run실행
public class ArrayMain08 {
public static void main(String[] args) {
if(args.length > 0) {
for (int i = 0; i <args.length; i++) {
System.out.println( i + " : " + args[i]);
}
}
else {
//전달되는 데이터가 없음
System.out.println("입력한 내용이 없습니다.");
}
}
}
main에 전달할 데이터는 두 개의 정수
main의 인자로 데이터를 전달하면 전달한 데이터의 자료형은 모두 String으로 인식하기 때문에 + 연산이 되지 않고 문자열의 연결이 된다.
String이 int로 변환되기 위해서는
Integer.parseInt(); 함수를 사용해준다.
public class ArrayMain09 {
public static void main(String[] args) {
System.out.println(args[0] + args[1]);
System.out.println("--------------------");
int num_1 = Integer.parseInt(args[0]);
int num_2 = Integer.parseInt(args[1]);
System.out.println("합계 : " + (num_1 + num_2 ));
}
}

public class ArrayMain10 {
public static void main(String[] args) {
//1. 배열의 선언, 생성, 초기화
int [] test = {10,20,30,40,50};
System.out.print(test[0] + "\t");
System.out.print(test[1]+ "\t");
System.out.print(test[2]+ "\t");
System.out.print(test[3]+ "\t");
System.out.println(test[4]);
System.out.println("-------------------");
//2. 반복문을 이용한 출력
for(int i = 0; i < test.length; i++) {
System.out.println("test[" + i + "] = " + test[i]);
}
System.out.println("-------------------");
//3. 확장 for문을 이용한 출력
for(int num : test) {
System.out.println(num);
}
System.out.println("-------------------");
//4. index 3의 데이터를 100으로 변경
test[3] = 100;
//5. 마지막 요소의 값을 200으로 변경
test[test.length -1] = 200;
for(int i = 0; i < test.length; i++) {
System.out.println(test[i]);
}
System.out.println("-------------------");
//6. 반복문을 사용하여 모든 요소의 값을 0으로 초기화
for(int i = 0; i < test.length; i++) {
test[i] = 0;
System.out.println(test[i]);
}
System.out.println("-------------------");
//7. 홀수 인덱스에 10 저장, 짝수 인덱스에 20저장
for(int i = 0; i < test.length; i ++) {
if (i % 2 == 0) {
test[i] = 20;
}
else {
test[i] = 10;
}
System.out.println(test[i]);
}
System.out.println("-------------------");
//8. 모든 요소의 총합과 평균
int sum = 0;
int avg = 0;
for(int i = 0; i < test.length; i++) {
sum += test[i];
avg = sum / test.length;
}
System.out.printf("모든 요소의 총합: %d%n" , sum);
System.out.printf("모든 요소의 평균: %d" , avg);
}
}
public class ArrayMain11 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
double sum = 0;
//double avg = 0;
System.out.print("요소의 개수를 정해주세요. >");
int num = input.nextInt();
double[] a = new double[num]; //double형 배열 선언
for(int i = 0; i < a.length; i++) {
System.out.print("a["+ i + "] = ");
a[i] = input.nextDouble();
sum += a[i];
//avg = sum / a.length;
//double input_a = input.nextDouble();
//sum += input_a;
}
System.out.println("모든 요소의 합은 " + sum + "입니다.");
//System.out.printf("모든 요소의 평균은 %.2f입니다." , avg);
System.out.printf("모든 요소의 평균은 %.2f입니다." , (sum / a.length) );
input.close();
}
}
[입력 예시]
단을 입력하세요 > 2
[출력 예시]
2단
----------
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
--
--
--
2 * 9 = 18
public class ArrayMain12 {
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.printf("%d 단%n" , dan);
System.out.println("-------------------");
//연산의 결과값을 저장할 배열 선언 및 생성
int [] array = new int [9];
for(int i = 0; i < array.length; i++) {
array[i] = dan * (i+1);
System.out.println( dan + " * " + (i+1) + " = " + array[i]);
}
input.close();
}
public class ArraySecondMain01 {
public static void main(String[] args) {
// 2차원 배열
int [] [] test; //선언
test = new int [2] [3]; //생성 ([2행] [3열])
// 행 열
test [0][0] = 100; // 초기화
test [0][1] = 200;
test [0][2] = 300;
test [1][0] = 400;
test [1][1] = 500;
test [1][2] = 600;
//배열의 요소 출력
System.out.println(test [0][0]);
System.out.println(test [0][1]);
System.out.println(test [0][2]);
System.out.println(test [1][0]);
System.out.println(test [1][1]);
System.out.println(test [1][2]);
System.out.println("================");
//반복문을 이용한 배열의 요소 출력
// 행의 길이
for (int i =0; i < test.length; i++){
for (int j =0; j < test[i].length; j++){
System.out.println( "test["+i+"]["+j+ "]"+ test[i][j]);
}
}
System.out.println("================");
//3행 3열의 2차원 배열을 선언, 생성
int[][] test2 = new int [3][3];
for (int i =0; i < test2.length; i++){
for (int j =0; j < test2[i].length; j++){
System.out.println(test2[i][j]);
}
}
System.out.println("================");
//2행 3열의 2차원 배열 선언, 생성(명시적 배열 생성), 초기화
int[][] test3 = new int[][] {{10, 20, 30},{40, 50, 60}};
for (int i =0; i < test3.length; i++){
for (int j =0; j < test3[i].length; j++){
System.out.println(test3[i][j]);
}
}
System.out.println("================");
//2행 3열의 2차원 배열 선언, 생성(암시적 배열 생성), 초기화
int[][] test4 = {{100, 200, 300},{400, 500, 600}};
for (int i =0; i < test4.length; i++){
for (int j =0; j < test4[i].length; j++){
System.out.println(test4[i][j]);
}
}
}
}
public class ArraySecondMain02 {
public static void main(String[] args) {
int[][] score = {{99, 98, 97}, {89, 72, 88}, {99, 97, 92} , {81,92,99} , {99,90,79}};
System.out.println("번호 국어 영어 수학 총점 평균");
System.out.println("==================");
// 행의 길이
for(int i =0; i < score.length; i++) {
int sum = 0;
System.out.print(" " + (i+1) + " ");
// 열의 길이
for(int j = 0; j<score[i].length; j++){
// 총점 구하기
sum += score[i][j];
// 과목 점수 출력
System.out.print(score[i][j] + " ");
}
// 총점 출력 및 평균 출력
System.out.println(sum + " " + (sum/score[i].length) );
}
}
}
public class ArraySecondMain03 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
//과목명
String[] course = {"국어", "영어", "수학"};
//인원수
System.out.println("인원 수를 입력해주세요 > ");
int num = input.nextInt();
// 인원수 과목수
// 행의 길이 열의 길이
int [][] score = new int [num] [course.length];
//총점& 평균을 저장하는 배열
int [] sum = new int [num];
float [] avg = new float [num];
// 성적을 입력 받고 총점과 평균을 구하기
// 행의 길이
for(int i = 0; i < score.length; i++) {
// 열의 길이
for(int j = 0; j < score[i].length; j++) {
// 성적 입력 받음
do {
//과목명 출력
System.out.print(course[j] + " = ");
score[i][j] = input.nextInt();
}
while(score[i][j] < 0 || score[i][j] > 100);
// 총점 구하기
sum[i] += score[i][j];
} //end of inner for
//평균 구하기
avg[i] = sum[i] / (float) score[i].length;
System.out.println();
}//end of outer for
//총점 & 평균 출력
// 인원수
for (int i = 0; i < num; i ++) {
System.out.println();
System.out.printf("총점 = %d%n" , sum[i]);
System.out.printf("평균 = %.2f%n" , avg[i]);
}
input.close();
}
}
package kr.s05.object.field;
public class Student01 {
// 멤버 필드(속성)
String name;
int age;
public static void main(String[] args) {
//객체 선언
Student01 student;
// 자료형
//객체 생성
// 생성자
student = new Student01();
//객체의 멤버 변수에 값을 할당
student.name = "홍길동"; //student 안에 객체의 주소가 들어가있음
student.age = 21;
//객체의 멤버 변수에 저장된 값을 출력
System.out.println("학생의 이름 : " + student.name );
System.out.println("학생의 나이 : " + student.age +"살");
}
}
package kr.s05.object.field;
public class Police01 {
//멤버 필드
String name;
int age;
public static void main(String[] args) {
// 객체 선언 및 생성
Police01 police;
police = new Police01();
//객체의 멤버 변수에 값 할당
police.name = "김유신";
police.age = 45;
//객체의 멤버 변수에 저장된 값을 읽기
System.out.println("경찰의 이름 = " + police.name);
System.out.println("경찰의 나이 = " + police.age + "살");
}
}