객체: 특징 (속성, 변수) / 행동 (메소드)
public class Main {
public static void main(String[] args) {
}
}
public: 제어자 👉 어디서든 접근할 수 있다
static: 이 프로그램이 시작될 때 무조건 실행되는 녀석
void: 메소드의 출력값의 데이터 타입
print
: 줄바꿈 X println
: 줄바꿈 O (ln = line)데이터값 뒤에 붙이는 구분값
long num = 100000000000L
float pi = 3.14F
\0
(널문자)가 없다.\0
(널문자)가 있다.int number = 21;
Integer num = new Integer(number);
이 방식은 이미 지원이 종료돼서 이런 식으로 사용하지 않고
Integer num = number;
이렇게 더 간단하게 사용한다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char letter = sc.nextLine().charAt(0);
int asciiNumber = (int)letter;
System.out.println(asciiNumber);
}
}
System.in
: 키보드 입력nextLine()
: '\n'
을 포함하는 한 라인을 읽고 '\n'
을 버린 나머지만 리턴 next()
: 다음 토큰을 문자열로 리턴charAt(0)
: 첫번째 글자만 받아온다. index = 0
: 변수의 타입을 바꾸는 방법
실수형 (float or double
) ➡️ 정수형 (int
)
3.141592.... ➡️ 3
소수점 아래는 버림
정수형 (int
) ➡️ 실수형 (float or double
)
31 ➡️ 31.0
.0 소수점이 생김
byte (1) -> short (2) -> int (4) -> long (8) -> float (4)
✔️ float
는 4byte이지만 부동소수점을 쓰고 있기 때문에 표현할 수 있는 식이 더 많다
작은 타입 ➡️ 큰 타입 형변환시 (자동 형변환)
큰 타입 ➡️ 작은 타입 형변환시 (강제 형변환 = 캐스팅)
ex) 입력 예시
백종원 돼지고기 김치찌개 만들기
4.5
돼지고기는 핏물을 빼주세요.
잘익은 김치 한포기를 꺼내서 잘라주세요.
냄비에 들기름 적당히 두르고 김치를 넣고 볶아주세요.
다진마늘 한스푼, 설탕 한스푼 넣어주세요.
종이컵으로 물 8컵 부어서 센불에 끓여주세요.
핏물 뺀 돼지고기를 넣어주세요.
된장 반스푼, 양파 반개, 청양고추 한개를 썰어서 넣어주세요.
간장 두스푼반, 새우젓 두스푼, 고춧가루 두스푼반 넣어주세요.
중불로 줄여서 오래 끓여주세요~!!
마지막에 파 쏭쏭 썰어서 마무리하면 돼요^^
예시 출력
[ 백종원 돼지고기 김치찌개 만들기 ]
별점 : 4 (80.0%)
1. 돼지고기는 핏물을 빼주세요.
2. 잘익은 김치 한포기를 꺼내서 잘라주세요.
3. 냄비에 들기름 적당히 두르고 김치를 넣고 볶아주세요.
4. 다진마늘 한스푼, 설탕 한스푼 넣어주세요.
5. 종이컵으로 물 8컵 부어서 센불에 끓여주세요.
6. 핏물 뺀 돼지고기를 넣어주세요.
7. 된장 반스푼, 양파 반개, 청양고추 한개를 썰어서 넣어주세요.
8. 간장 두스푼반, 새우젓 두스푼, 고춧가루 두스푼반 넣어주세요.
9. 중불로 줄여서 오래 끓여주세요~!!
10. 마지막에 파 쏭쏭 썰어서 마무리하면 돼요^^
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 요리제목
String title = sc.next();
// 별점 & 퍼센트
float score = sc.nextFloat();
int intScore = (int)score;
float percent;
percent = (float) (score * 100 / 5.0);
String resultPercent = String.format("%.1f", percent);
// 레시피
String[] recipe = new String[10];
sc.nextLine();
for(int i = 0; i < recipe.length; i++){
recipe[i] = sc.nextLine();
}
// 모든 입력값 출력
System.out.println("[ " + title + " ]");
System.out.println("별점 : " + intScore + " (" + resultPercent + "%)");
for(int i = 0; i < recipe.length; i++){
System.out.println(i+1 + ". " + recipe[i]);
}
}
}
나는 10번을 반복하기 싫어서 반복문을 이용하여 문제를 풀어보았다.
import java.util.Scanner;
public class Main01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String title = sc.nextLine();
float rate = sc.nextFloat();
String input1 = sc.nextLine();
String input2 = sc.nextLine();
String input3 = sc.nextLine();
String input4 = sc.nextLine();
String input5 = sc.nextLine();
String input6 = sc.nextLine();
String input7 = sc.nextLine();
String input8 = sc.nextLine();
String input9 = sc.nextLine();
String input10 = sc.nextLine();
title = "[ " + title + " ]"; // [ 제목 ]
System.out.println(title);
int intRate = (int)rate; // 강제 형변환
System.out.println(intRate);
double percentageRate = intRate * 100 / 5.0; // 자동 형변환
System.out.println(percentageRate);
System.out.println("1." + input1);
System.out.println("2." + input2);
System.out.println("3." + input3);
System.out.println("4." + input4);
System.out.println("5." + input5);
System.out.println("6." + input6);
System.out.println("7." + input7);
System.out.println("8." + input8);
System.out.println("9." + input9);
System.out.println("10." + input10);
}
}