Post

[JAVA] 한 페이지에 싹 정리

[JAVA] 한 페이지에 싹 정리

정보처리 기능사 실기 ㄱ같이 말아먹고 정리하는 자바…ㅅ.

기본 베이스

콘솔 출력

JAVA 에서 ‘System.out.println() 을 사용해 콘솔에 출력함

1
2
System.out.println("Hellow World!");     // Hellow World  
System.out.println(12);                  // 12  

주석처리

주석처리 예임

1
2
// 한 줄 주석
/* 여러 줄 주석 */

이건 다른 언어와 비슷한듯..

변수

JAVA는 정적 타입 언어라, 변수 선언 시 자료형을 지정해야 함

1
2
int burgerPrice = 4990;
String userName = "Emily";

함수 (메서드)

public static 을 사용해 클래스에 직접 호출 가능

1
2
3
4
5
6
public static void greet(String name, String nationality){
    system.out.println(name);          // 홍길동  
    system.out.println(nationality);   // 한국  
}

greet("홍길동","한국");

자료형(Data Type)

숫자형

다른 언어와 마찬가지로 int(정수), double(실수) 있음

1
2
int a = -1;
double b = 3.14;

숫자 연산

1
2
3
4
5
int sum = 7 + 4;            // 11  
int diff = 7 - 4;           // 3  
int prod = 7 * 4;           // 21  
double quot = 7.0 / 4.0;    // 1.0  
int mod = 7 % 4;            // 3

별개로.. Math.pow

1
2
3
4
5
6
7
8
9
public class Main{
  public static void main(String[] args){
    double result1 = Math.pow(2, 3);         // 2의 3제곱 = 2^3  
    System.out.println("2^3 = " + result1);  // 2^3 = 8.0  (double 이기에..)

    double result2 = Math.pow(10, -1);
    System.out.println("10^-1 =" + result2); // 10^-1 = 0.1
  }
}

pow 는 제곱을 나타낸다

문자열

문자의 연속, ‘+’ 연산자로 연결 가능함

1
2
3
4
5
String messge = "Hello World";
String name = "전우치";
int age = 201;
System.out.println("제 이름은 %s이고 %d살입니다.",name,age);
// 제 이름은 전우치이고 201살입니다.  

불린형(Boolean)

1
2
boolean isJavaFun = True;
boolean isFishTasty = false;

배열과 컬렉션

배열

1
2
int[] numbers = {1,2,3,4,5};
System.out.println(numbers[0]);   // 1

리스트(ArrayList)

1
2
3
4
5
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
names.add("카리나");
names.add("윈터");
System.out.println(names.get(0));   // "카리나"

해시맵(HashMap)

키 -값 쌍을 저장하는 자료구조임

1
2
3
4
5
import java.util.HashMap;
HashMap<String,Integer> scores = new HashMap<>();
scores.put("수학",90);
scores.put("영어",85);
System.out.println(scores.get("수학"));    // 90  

제어문

if 조건문

1
2
3
4
5
6
7
8
int score = 85;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("F");
}

while 반복문

조건이 참일 때 계속 실행됨

1
2
3
4
5
int i = 1;
while (i <= 3) {
    System.out.println("난 할 수 있다");
    i++;
}

for 반복문

1
2
3
4
5
6
7
for (int j = 0; j < 5; j++) {
    System.out.println(j);
}
// 1
// 2
// 3
// 4

사용자 입력

C에서 ‘scanf’이던거. JAVA에서 ‘Scanner’

import java.util.Scanner;
Scanner scanner =  new Scanner(System.in);
System.out.print("숫자를 입력하세요: ");
int num = scanner.nextInt();
System.out.println("입력한 숫자: " + num);
// 입력한 숫자: num

파일 읽고 쓰기

파일 쓰기

FileWriter 클래스를 사용해 파일 데이터 쓸 수 있음

1
2
3
4
5
6
7
import java.io.FileWriter;
import java.io.IOException;
FileWriter writer = new FileWriter("text_file.text");
writer.write("Hellow World! \n");
writer.close();
// text_file.text 파일을 열어
// Hellow World 입력후 닫음

파일 읽기

Scanner 클래스 이용해 파일 읽음

1
2
3
4
5
6
7
8
9
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
File file - new File("text_file.txt");
Scanner readeer = new Scanner(file);      // reader 파일의 내용을 읽을 준비 함
while (reader.hasNextLine()) {            // 파일에 읽을 다음 줄이 있는지 확인하는 메서드, true-반복문 실행,  false-종료  
  System.out.println(reader.nextLine());  // nextLine() : 파일에서 한 줄을 읽어 문자열로 반환  
}                                         // System.out.println() : 읽은 줄은 콘솔에 출력  
reader.close();
This post is licensed under CC BY 4.0 by the author.