โ ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ์ ๋ ๋ฌธ์์ด์ด ๊ฐ์์ง ๋น๊ตํ์ธ์.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ ์
๋ ฅํ์ธ์: ");
String str1 = scanner.nextLine();
System.out.print("๋ ๋ฒ์งธ ๋ฌธ์์ด์ ์
๋ ฅํ์ธ์: ");
String str2 = scanner.nextLine();
boolean result = str1.equals(str2);
System.out.println("๋ ๋ฌธ์์ด์ด ๊ฐ์๊ฐ์? " + result);
}
}
๐ ์คํ ๊ฒฐ๊ณผ
์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ ์
๋ ฅํ์ธ์: hello
๋ ๋ฒ์งธ ๋ฌธ์์ด์ ์
๋ ฅํ์ธ์: world
๋ ๋ฌธ์์ด์ด ๊ฐ์๊ฐ์? false
โ ๋ ์ซ์์ ํฌ๊ธฐ๋ฅผ ๋น๊ตํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ์ธ์.
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println("x๊ฐ y๋ณด๋ค ํฐ๊ฐ? " + (x > y));
System.out.println("x๊ฐ y๋ณด๋ค ์์๊ฐ? " + (x < y));
System.out.println("x์ y๊ฐ ๊ฐ์๊ฐ? " + (x == y));
System.out.println("x์ y๊ฐ ๋ค๋ฅธ๊ฐ? " + (x != y));
}
}
๐ ์คํ ๊ฒฐ๊ณผ
x๊ฐ y๋ณด๋ค ์์๊ฐ? true
x์ y๊ฐ ๊ฐ์๊ฐ? false
x์ y๊ฐ ๋ค๋ฅธ๊ฐ? true
โ ์๋ 5๋ช ์ ๋ง์ดํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์. ๋จ, 4๋ฒ์งธ ์๋์์ ์ค๋จํ์ธ์.
public class Robot {
public static void main(String[] args) {
int customers = 5;
for (int i = 1; i <= customers; i++) {
if (i == 4) {
System.out.println("4๋ฒ์งธ ์๋ ๋์ฐฉ! ํ๋ก๊ทธ๋จ ์ข
๋ฃ.");
break;
}
System.out.println(i + "๋ฒ์งธ ์๋, ์๋
ํ์ธ์!");
}
}
}
๐ ์คํ ๊ฒฐ๊ณผ
1๋ฒ์งธ ์๋, ์๋
ํ์ธ์!
2๋ฒ์งธ ์๋, ์๋
ํ์ธ์!
3๋ฒ์งธ ์๋, ์๋
ํ์ธ์!
4๋ฒ์งธ ์๋ ๋์ฐฉ! ํ๋ก๊ทธ๋จ ์ข
๋ฃ.
โ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ซ์์ ๊ตฌ๊ตฌ๋จ์ ์ถ๋ ฅํ์ธ์.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("์ถ๋ ฅํ ๊ตฌ๊ตฌ๋จ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์ (2~9): ");
int dan = scanner.nextInt();
if (dan < 2 || dan > 9) {
System.out.println("โ ๏ธ 2์์ 9 ์ฌ์ด์ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์!");
} else {
System.out.println("\n==== " + dan + "๋จ ====");
for (int i = 1; i <= 9; i++) {
System.out.println(dan + " x " + i + " = " + (dan * i));
}
}
}
}
๐ ์คํ ๊ฒฐ๊ณผ
์ถ๋ ฅํ ๊ตฌ๊ตฌ๋จ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์ (2~9): 3
==== 3๋จ ====
3 x 1 = 3
3 x 2 = 6
...
3 x 9 = 27
โ "์๋์ฐจ(Car)" ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ์๋์ฐจ๊ฐ ์ฃผํํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ธ์.
package chapter2.clazz;
public class Car {
String name;
String carNumber;
public Car(String name, String carNumber) {
this.name = name;
this.carNumber = carNumber;
}
public void drive() {
System.out.println(name + " (" + carNumber + ") ์ฃผํ ์ค!");
}
}
package chapter2.clazz;
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Tesla Model Y", "1234-ABCD");
myCar.drive();
}
}
๐ ์คํ ๊ฒฐ๊ณผ
Tesla Model Y (1234-ABCD) ์ฃผํ ์ค!
public class MyDouble {
private double value;
public MyDouble(double value) {
this.value = value;
}
public MyDouble add(MyDouble other) {
return new MyDouble(this.value + other.value);
}
public double getValue() {
return value;
}
}
public class DoubleWrapperPerformance {
public static void main(String[] args) {
int iteration = 10_000_000; // 1000๋ง ๋ฒ ๋ฐ๋ณต
// 1. ๊ธฐ๋ณธํ double ์ฐ์ฐ
long startTime1 = System.nanoTime();
double sum1 = 0.0;
for (int i = 0; i < iteration; i++) {
sum1 += i * 1.1;
}
long endTime1 = System.nanoTime();
long primitiveTime = endTime1 - startTime1;
// 2. MyDouble ์ฐ์ฐ
long startTime3 = System.nanoTime();
MyDouble sum3 = new MyDouble(0.0);
for (int i = 0; i < iteration; i++) {
sum3 = sum3.add(new MyDouble(i * 1.1));
}
long endTime3 = System.nanoTime();
long myDoubleTime = endTime3 - startTime3;
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
System.out.println("๊ธฐ๋ณธํ(double) ์ฐ์ฐ ์๊ฐ: " + primitiveTime + " ns");
System.out.println("MyDouble ํด๋์ค ์ฐ์ฐ ์๊ฐ: " + myDoubleTime + " ns");
System.out.println("MyDouble vs double ์ฑ๋ฅ ์ฐจ์ด: " + (double) myDoubleTime / primitiveTime);
}
}
class MyDouble {
private double value;
public MyDouble(double value) {
this.value = value;
}
public MyDouble add(MyDouble other) {
return new MyDouble(this.value + other.value);
}
public double getValue() {
return value;
}
}
๐ ์คํ ๊ฒฐ๊ณผ
๊ธฐ๋ณธํ(double) ์ฐ์ฐ ์๊ฐ: 650000000 ns
MyDouble ํด๋์ค ์ฐ์ฐ ์๊ฐ: 1800000000 ns
MyDouble vs double ์ฑ๋ฅ ์ฐจ์ด: 2.769230769230769
โ ๋ค์ํ ๊ฐ์ ์ ํ์ ์ผ๊ด๋ ๋ฐฉ์์ผ๋ก ์กฐ์ํ ์ ์๋ ์ธํฐํ์ด์ค๋ฅผ ์ค๊ณํ์ธ์.
// ๊ฐ์ ์ ํ ์ ์ํ ์ธํฐํ์ด์ค
interface ElectronicDevice {
void turnOn(); // ์ ์ ์ผ๊ธฐ ๊ธฐ๋ฅ
void turnOff(); // ์ ์ ๋๊ธฐ ๊ธฐ๋ฅ
}
// TV ํด๋์ค๊ตฌํ
class TV implements ElectronicDevice {
@Override
void turnOn() {
System.out.println("TV ์ ์์ด ์ผ์ก์ต๋๋ค.");
}
@Override
void turnOff() {
System.out.println("TV ์ ์์ด ๊บผ์ก์ต๋๋ค.");
}
// ์ถ๊ฐ ๊ธฐ๋ฅ
void changeChannel() {
System.out.println("์ฑ๋์ ๋ณ๊ฒฝํฉ๋๋ค.");
}
}
// ์์ด์ปจ ํด๋์ค (ElectronicDevice ๊ตฌํ)
class AirConditioner implements ElectronicDevice {
@Override
void turnOn() {
System.out.println("์์ด์ปจ์ด ๊ฐ๋๋ฉ๋๋ค.");
}
@Override
void turnOff() {
System.out.println("์์ด์ปจ์ด ๊บผ์ก์ต๋๋ค.");
}
// ์ถ๊ฐ ๊ธฐ๋ฅ (์ธํฐํ์ด์ค์๋ ์๋ ๊ธฐ๋ฅ)
void setTemperature() {
System.out.println("์จ๋๋ฅผ ์ค์ ํฉ๋๋ค.");
}
}
// ์ธํ๊ธฐ ํด๋์ค (ElectronicDevice ๊ตฌํ)
class WashingMachine implements ElectronicDevice {
@Override
void turnOn() {
System.out.println("์ธํ๊ธฐ๊ฐ ์๋์ ์์ํฉ๋๋ค.");
}
@Override
void turnOff() {
System.out.println("์ธํ๊ธฐ๊ฐ ์๋์ ๋ฉ์ถฅ๋๋ค.");
}
// ์ถ๊ฐ ๊ธฐ๋ฅ (์ธํฐํ์ด์ค์๋ ์๋ ๊ธฐ๋ฅ)
void setTime() {
System.out.println("์ธํ ์๊ฐ์ ์ค์ ํฉ๋๋ค.");
}
}
public class Main {
public static void main(String[] args) {
// โ
๊ฐ์ ์ ํ ๊ฐ์ฒด ์์ฑ
TV tv = new TV();
AirConditioner airConditioner = new AirConditioner();
WashingMachine washingMachine = new WashingMachine();
// โ
๊ฐ๋ณ์ ์ผ๋ก ์ ์ ์ผ๊ธฐ
System.out.println(" ๊ฐ์ ์ ํ์ ์ผญ๋๋ค.");
tv.turnOn();
airConditioner.turnOn();
washingMachine.turnOn();
System.out.println(); // ์ค๋ฐ๊ฟ
// โ
์ถ๊ฐ ๊ธฐ๋ฅ ์ฌ์ฉ
tv.changeChannel();
airConditioner.setTemperature();
washingMachine.setTime();
System.out.println(); // ์ค๋ฐ๊ฟ
// โ
๊ฐ๋ณ์ ์ผ๋ก ์ ์ ๋๊ธฐ
System.out.println(" ๊ฐ์ ์ ํ์ ๋๋๋ค.");
tv.turnOff();
airConditioner.turnOff();
washingMachine.turnOff();
}
}
๐ ์คํ ๊ฒฐ๊ณผ
๊ฐ์ ์ ํ์ ์ผญ๋๋ค.
TV ์ ์์ด ์ผ์ก์ต๋๋ค.
์์ด์ปจ์ด ๊ฐ๋๋ฉ๋๋ค.
์ธํ๊ธฐ๊ฐ ์๋์ ์์ํฉ๋๋ค.
์ฑ๋์ ๋ณ๊ฒฝํฉ๋๋ค.
์จ๋๋ฅผ ์ค์ ํฉ๋๋ค.
์ธํ ์๊ฐ์ ์ค์ ํฉ๋๋ค.
๊ฐ์ ์ ํ์ ๋๋๋ค.
TV ์ ์์ด ๊บผ์ก์ต๋๋ค.
์์ด์ปจ์ด ๊บผ์ก์ต๋๋ค.
์ธํ๊ธฐ๊ฐ ์๋์ ๋ฉ์ถฅ๋๋ค.
โ ํด๋์ค๋ฅผ ํ์ฉํด์ ์ ๋ด์ฉ์ 3๋จ๊ณ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ์ค๊ณํ์ธ์.
// LifeForm ์ธํฐํ์ด์ค
package chapter2.polymorphism;
public interface LifeForm {
void exist();
}
// Animal ์ธํฐํ์ด์ค LifeForm ์์
package chapter2.polymorphism;
public interface Animal extends LifeForm {
void makeSound();
}
// Cat ํด๋์ค
package chapter2.polymorphism;
public class Cat implements Animal {
@Override
public void exist() {
System.out.println("๊ณ ์์ด๊ฐ ์กด์ฌํฉ๋๋ค");
}
@Override
public void makeSound() {
System.out.println("์ผ์น");
}
public void scratch() {
System.out.println("์คํฌ๋ ์น");
}
}
// Dog ํด๋์ค
package chapter2.polymorphism;
public class Dog implements Animal {
@Override
public void exist() {
System.out.println("๊ฐ์์ง๊ฐ ์กด์ฌํฉ๋๋ค");
}
@Override
public void makeSound() {
System.out.println("๋ฉ๋ฉ");
}
public void wag() {
System.out.println("ํ๋คํ๋ค");
}
}
// Main ํด๋์ค
package chapter2.polymorphism;
public class Main {
public static void main(String[] args) {
// โ
๋ค์์ฑ ํ์ฉ
Animal animal1 = new Cat();
Animal animal2 = new Dog();
animal1.exist();
animal1.makeSound();
animal2.exist();
animal2.makeSound();
// โ
๋ค์ด์บ์คํ
Cat cat = (Cat) animal1;
cat.scratch();
Dog dog = (Dog) animal2;
dog.wag();
// โ
๋ค์ด์บ์คํ
instanceof ํ์ฉ
if (animal2 instanceof Cat) {
Cat cat2 = (Cat) animal2;
cat2.scratch();
} else {
System.out.println("๊ฐ์ฒด๊ฐ ๊ณ ์์ด๊ฐ ์๋๋๋ค");
}
}
}
๐ ์คํ ๊ฒฐ๊ณผ
๊ณ ์์ด๊ฐ ์กด์ฌํฉ๋๋ค
์ผ์น
๊ฐ์์ง๊ฐ ์กด์ฌํฉ๋๋ค
๋ฉ๋ฉ
์คํฌ๋ ์น
ํ๋คํ๋ค
๊ฐ์ฒด๊ฐ ๊ณ ์์ด๊ฐ ์๋๋๋ค
โ ์ฅ๋ฐ๊ตฌ๋ ๊ตฌํํ๊ธฐ
addProduct
)printCart
)removeProduct
)calculateTotalPrice
)public class Product {
// ์์ฑ
private String name;
private int price;
// ์์ฑ์
public Product(String name, int price) {
this.name = name;
this.price = price;
}
// ๊ธฐ๋ฅ
public String getName() {
return this.name;
}
public int getPrice() {
return this.price;
}
}
import java.util.ArrayList;
import java.util.List;
public class Cart {
// ์์ฑ
private List<Product> cart = new ArrayList<>();
// ์์ฑ์
// ๊ธฐ๋ฅ
// ์ํ ์ถ๊ฐ ๊ธฐ๋ฅ
public void addProduct(Product product) {
cart.add(product);
System.out.println(product.getName() + " ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ ๋์์ต๋๋ค.");
}
// ์ํ ์ ๊ฑฐ ๊ธฐ๋ฅ
public void removeProduct(String removeProductName) {
boolean removed = false;
for (Product product : cart) {
String foundProductName = product.getName();
if (foundProductName.equals(removeProductName)) {
cart.remove(product);
System.out.println(product.getName() + "๊ฐ ์ฅ๋ฐ๊ตฌ๋์์ ์ ๊ฑฐ ๋์์ต๋๋ค.");
break;
}
}
if (!removed) {
System.out.println("ํด๋น ์ํ์ด ์ฅ๋ฐ๊ตฌ๋์ ์์ต๋๋ค.");
}
}
// ์ฅ๋ฐ๊ตฌ๋ ๋ชฉ๋ก ์ถ๋ ฅ ๊ธฐ๋ฅ
public void printCart() {
if (cart.isEmpty()) {
System.out.println("์ฅ๋ฐ๊ตฌ๋๊ฐ ๋น์ด ์์ต๋๋ค.");
} else {
for (Product product : cart) {
System.out.println(product.getName() + ": " + product.getPrice());
}
}
}
// ์ฅ๋ฐ๊ตฌ๋ ์ด ์ํ ๊ฐ๊ฒฉ ์กฐํ ๊ธฐ๋ฅ
public void calculateTotalPrice() {
int total = 0;
for (Product product : cart) {
total += product.getPrice();
}
System.out.println("์ด ๊ธ์ก์: " + total);
}
}
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
Product onion = new Product("์ํ", 3000);
Product apple = new Product("์ฌ๊ณผ", 10000);
Product fish = new Product("์์ ", 12000);
Product tofu = new Product("๋๋ถ", 2000);
// ์ฅ๋ฐ๊ตฌ๋์ ์ํ ์ถ๊ฐ
System.out.println("์ฅ๋ฐ๊ตฌ๋ ์ํ ์ถ๊ฐ: ");
cart.addProduct(onion);
cart.addProduct(apple);
cart.addProduct(fish);
cart.addProduct(tofu);
System.out.println();
// ์ฅ๋ฐ๊ตฌ๋ ์ํ ์กฐํ
System.out.println("์ฅ๋ฐ๊ตฌ๋ ์ํ ์กฐํ: ");
cart.printCart();
System.out.println();
// ์ฅ๋ฐ๊ตฌ๋ ์ด ๊ธ์ก ์กฐํ
System.out.println("์ฅ๋ฐ๊ตฌ๋ ์ด ๊ธ์ก ์กฐํ: ");
cart.calculateTotalPrice();
System.out.println();
// ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ ๊ฑฐ(์ฌ๊ณผ)
System.out.println("์ฅ๋ฐ๊ตฌ๋์์ ์ฌ๊ณผ ์ ๊ฑฐ: ");
cart.removeProduct("์ฌ๊ณผ");
System.out.println();
// ์ฅ๋ฐ๊ตฌ๋ ์ํ ์กฐํ
System.out.println("์ฅ๋ฐ๊ตฌ๋ ์ํ ์กฐํ: ");
cart.printCart();
System.out.println();
// ์ฅ๋ฐ๊ตฌ๋ ์ด ๊ธ์ก ์กฐํ
System.out.println("์ฅ๋ฐ๊ตฌ๋ ์ด ๊ธ์ก ์กฐํ: ");
cart.calculateTotalPrice();
System.out.println();
}
}
๐ ์คํ ๊ฒฐ๊ณผ
์ฅ๋ฐ๊ตฌ๋ ์ํ ์ถ๊ฐ:
์ํ ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ ๋์์ต๋๋ค.
์ฌ๊ณผ ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ ๋์์ต๋๋ค.
์์ ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ ๋์์ต๋๋ค.
๋๋ถ ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ ๋์์ต๋๋ค.
์ฅ๋ฐ๊ตฌ๋ ์ํ ์กฐํ:
์ํ: 3000
์ฌ๊ณผ: 10000
์์ : 12000
๋๋ถ: 2000
์ฅ๋ฐ๊ตฌ๋ ์ด ๊ธ์ก ์กฐํ:
์ด ๊ธ์ก์: 27000
์ฅ๋ฐ๊ตฌ๋์์ ์ฌ๊ณผ ์ ๊ฑฐ:
์ฌ๊ณผ๊ฐ ์ฅ๋ฐ๊ตฌ๋์์ ์ ๊ฑฐ ๋์์ต๋๋ค.
์ฅ๋ฐ๊ตฌ๋ ์ํ ์กฐํ:
์ํ: 3000
์์ : 12000
๋๋ถ: 2000
์ฅ๋ฐ๊ตฌ๋ ์ด ๊ธ์ก ์กฐํ:
์ด ๊ธ์ก์: 15000
โ์ ์ num1๊ณผ num2๊ฐ ์ฃผ์ด์ง ๋, num1๊ณผ num2์ ํฉ์ returnํ๋๋ก soltuion ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
class Solution {
public int solution(int num1, int num2) {
return num1 + num2;
}
}
class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution(2,3));
System.out.println(sol.solution(100,2));
}
}
โ ์ ์ num1๊ณผ num2๊ฐ ์ฃผ์ด์ง ๋, num1์์ num2๋ฅผ ๋บ ๊ฐ์ returnํ๋๋ก soltuion ํจ์๋ฅผ ์์ฑ ํ๊ธฐ
class Solution {
public int solution(int num1, int num2) {
return num1 - num2; // ๋ ์ ์๋ฅผ ๋ฐ์ ๋นผ๋ ๊ฐ์ ๋ฐํํ๋ ๋ฉ์๋
}
}
class main {
public static void main(String[] args) {
Solution sol = new Solution(); // ํด๋์ค ๊ฐ์ฒด๋ฅผ ์์ฑ
System.out.println(sol.solution(2, 3)); //๋ฉ์๋(solution)๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ ๊ฐ ์ถ๋ ฅ
System.out.println(sol.solution(100, 2));
}
}
๐ ์คํ ๊ฒฐ๊ณผ
num1์ด 2์ด๊ณ num2๊ฐ 3์ด๋ฏ๋ก 2 - 3 = -1์ returnํฉ๋๋ค.
num1์ด 100์ด๊ณ num2๊ฐ 2์ด๋ฏ๋ก 100 - 2 = 98์ returnํฉ๋๋ค.
โ ์ ์ num1, num2๊ฐ ์ฃผ์ด์ง๋ num1๊ณผ num2๋ฅผ ๊ณฑํ ๊ฐ์ returnํ๋๋ก soltuion ํจ์๋ฅผ ์์ฑํ๊ธฐ
โ static ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง ์๊ณ ์์ฑ
โ void ๋ฐํ๊ฐ์ด ์์ (return ๊ฐ์ด ํ์ ์์)
class Solution {
public int solution(int num1, int num2) {
return num1 * num2; // ๋ ์ ์๋ฅผ ๋ฐ์ ๊ณฑํ ๊ฐ์ ๋ฐํํ๋ ๋ฉ์๋
}
}
class main {
public static void main(String[] args) { //
Solution sol = new Solution(); // ํด๋์ค ๊ฐ์ฒด๋ฅผ ์์ฑ
System.out.println(sol.solution(3, 4)); //๋ฉ์๋(solution)๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ ๊ฐ ์ถ๋ ฅ
System.out.println(sol.solution(27, 19));
}
}
โ ์ ์ num1, num2๊ฐ ์ฃผ์ด์ง๋ num1๊ณผ num2๋ฅผ ๋๋ ๊ฐ์ returnํ๋๋ก soltuion ํจ์๋ฅผ ์์ฑํ๊ธฐ
class Solution {
public int solution(int num1, int num2) {
return num1 / num2; // ์ ์ ๋๋์
}
}
class Main {
public static void main(String[] args) {
Solution sol = new Solution(); // ํด๋์ค ๊ฐ์ฒด๋ฅผ ์
System.out.println(sol.solution(10, 5));//๋ฉ์๋(solution)๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ ๊ฐ
System.out.println(sol.solution(7, 2))
}
}
โ ๋จธ์ฑ์ด๋ ์ ์๋์ด ๋ช ๋
๋์ ํ์ด๋ฌ๋์ง ๊ถ๊ธํด์ก์ต๋๋ค. 2022๋
๊ธฐ์ค ์ ์๋์ ๋์ด age๊ฐ ์ฃผ์ด์ง ๋, ์ ์๋์ ์ถ์ ์ฐ๋๋ฅผ return ํ๋ solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์
โ 2022๋
ํ์ด๋ ์๊ฐ๋ถํฐ 1์ด์ด๋ฏ๋ก +1
class Solution {
public int solution(int age) {
return 2022 - age + 1; // 2022๋
๊ธฐ์ค ์ผ๋ก (ํ์ฌ ์ฐ๋ - ๋์ด) + 1
}
}
class Main {
public static void main(String[] args) {
Solution sol = new Solution(); // ํด๋์ค ๊ฐ์ฒด๋ฅผ ์์ฑ
System.out.println(sol.solution(40)); //๋ฉ์๋(solution)๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ ๊ฐ
System.out.println(sol.solution(25));
}
}
โ ์ ์ num1๊ณผ num2๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋๋ค. ๋ ์๊ฐ ๊ฐ์ผ๋ฉด 1 ๋ค๋ฅด๋ฉด -1์ retrunํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
โ if ๋ง์ฝ์ ~ํ๋ฉด ๊ฐ์ ์กฐ๊ฑด์ผ๋ก ํ์ธ
โ == ๋์๊ฐ ๊ฐ์์ง
class Solution {
public int solution(int num1, int num2) {
if (num1 == num2) { // num1๊ณผ num2๊ฐ ๊ฐ์ผ๋ฉด
return 1; //1์ ๋ฐํ
} else { //๊ฐ์ง์์ผ๋ฉด
return -1; //-1์ ๋ฐํ
}
}
}
โ ์ ์ num1๊ณผ num2๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, num1์ num2๋ก ๋๋ ๊ฐ์ 1,000์ ๊ณฑํ ํ ์ ์ ๋ถ๋ถ์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
โ ์ค์ ๋๋์
๋จผ์ ์ํํ 1000์ ๊ณฑํจ
โ ๊ตฌํ๊ฐ์ ์ ์๋ก ๋ฐํ
class Solution {
public int solution(int num1, int num2) {
return (int) (((double) num1 / num2) * 1000); // ์ค์ ๋๋์
์ ((๋จผ์ ์ํ 1000์ ๊ณฑํด์ ๊ฐ ๊ตฌํ๊ธฐ, ๊ตฌํ๊ฐ์ ์ ์๊ฐ ๋ฐํ
}
}
class Main {
public static void main(String[] args) {
Solution sol = new Solution(); // ํด๋์ค ๊ฐ์ฒด๋ฅผ ์์ฑ
System.out.println(sol.solution(3, 2)); ////๋ฉ์๋(solution)๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ ๊ฐ
System.out.println(sol.solution(7,3));
System.out.println(sol.solution(1,16));
}
}