MapleStory Finger Point

🟀 JAVA/πŸ“šλͺ…ν’ˆ JAVA_ESSENTIAL

[λͺ…ν’ˆ JAVA ESSENTIAL] CH6 μ‹€μŠ΅λ¬Έμ œ

HYEJU01 2021. 11. 16. 16:30
"κ°œμ •νŒ: λͺ…ν’ˆ JAVAESSENTIAL(ν™©κΈ°νƒœ)"κ΅μž¬μ— 수둝된 λ¬Έμ œλ“€μ„ ν’€μ–΄μ„œ μ˜¬λ¦½λ‹ˆλ‹€.
문제의 번호만 ν‘œκΈ°ν•˜κ³  λ‹΅μ•ˆλ§Œ μ λŠ” ν˜•νƒœλ‘œ μ—…λ‘œλ“œ ν•˜κ³ μžˆμŠ΅λ‹ˆλ‹€.
개인 ν’€μ΄μ΄λ―€λ‘œ μ˜€λ‹΅μ΄ μžˆμ„ 수 있으며 μ˜€λ‹΅ 발견 μ‹œ λŒ“κΈ€ λ‚¨κ²¨μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€πŸ˜Š
-
#이 ν‘œμ‹œλœ 건 아직 풀지 λͺ»ν•œ 문제 or ν—·κ°ˆλ¦¬λŠ” λ¬Έμ œμž…λ‹ˆλ‹€!
좔후에 λ‹€μ‹œ ν’€μ–΄μ„œ 올릴 μ˜ˆμ •μ΄μ—μš”!

-ujeyhx-

 

πŸ”Žμ‹€μŠ΅λ¬Έμ œ

 

1) 

class MyPoint{
	private int x,y;
	public MyPoint(int x, int y){
	this.x = x;
	this.y = y;
	}
	
	public String toString() {
		return "MyPoint(" + x + "," + y + ")";
	}
	
}

public class TEST {
	public static void main (String[] args) {
		MyPoint a = new MyPoint(3,20);
		System.out.println(a);
	}
}

 

 


2)

import java.util.Scanner;
import java.util.StringTokenizer;

public class TEST {
	public static void main (String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		while(true) {
		System.out.print("μž…λ ₯ν•˜μ„Έμš”>");
		String str = sc.nextLine(); //ν•œλΌμΈ 읽기
		StringTokenizer st = new StringTokenizer(str," ");
		if (str.equals("exit")) {
			System.out.println(" μ’…λ£Œν•©λ‹ˆλ‹€ ");
			break;
		}
		int n = st.countTokens();
		System.out.println("μ–΄μ ˆ κ°œμˆ˜λŠ” " + n);

		} 

	}
}

 


3)

public class TEST {
	public static void main (String[] args) {
		
		int[] val = new int[3];
		
		while (true) {

		for (int i= 0; i<3; i++) {
			val[i] = (int)(Math.random()*3+1);
			System.out.print(val[i] + "\t"); //1λΆ€ν„° 3κΉŒμ§€ λ‚œμˆ˜ 생성);
			
		}
		System.out.println();
		if(val[0] == val[1] && val[1] == val[2]) {
			System.out.print("성곡");
			break;
		}
		
		}
	}
}

 

next()κ°€ μ•„λ‹ˆλΌ nextLine()을 μ‚¬μš©ν•΄μ•Όν•œλ‹€

4)

import java.util.Scanner;
import java.util.StringTokenizer;

public class TEST {
	public static void main (String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.print("λ§μ…ˆμ‹μ„ μž…λ ₯ν•˜μ„Έμš”>");
		String str = sc.nextLine(); //ν•œλΌμΈ 읽기

		StringTokenizer st = new StringTokenizer(str,"+");
		int sum = 0;
		int n = 0;
		while(st.hasMoreTokens()) {
			String str1 = st.nextToken();
			str1 = str1.trim();
			n = Integer.parseInt(str1);
			sum += n;
		}
			System.out.print("합은 " + sum);
	}
}

 

 

trim 은 슀트링 μ•žλ’€ 곡백만 μ—†μ• μ£Όλ―€λ‘œ trim을 μ‚¬μš©ν• λ•Œμ—λŠ” "1 "," 3 "," 5" ν† ν°λ³„λ‘œ trim 을 ν•΄μ€˜μ•Όν•œλ‹€. 

5)

package util;

public class Adder{
	private int x,y;
	public Adder(int x, int y) { this.x = x; this.y = y;}
	public int add() { return x+y;}
}
package app;
import util.Adder;

public class Main {
	public static void main (String[] args) {
		
	Adder adder = new Adder(2,5);
	System.out.println(adder.add());
}
}


6)

import java.util.Scanner;

public class TEST {
	public static void main (String[] args) {
		
	int num = 0;
	int num2 = 0;
	String[] val = {" ", "κ°€μœ„", "λ°”μœ„", "보"};
	Scanner sc = new Scanner(System.in);

	
	while(true) {
		System.out.println("κ°€μœ„(1), λ°”μœ„(2), 보(3), 끝내기(4)>>");
		num = sc.nextInt();
		num2 = (int) (Math.random()*3+1);

	
	if (num == 1) {
		System.out.println("μ‚¬μš©μž " + val[num] + " : 컴퓨터 " + val[num2]);
		if (num2 == 1)
			System.out.println("λΉ„κ²ΌμŠ΅λ‹ˆλ‹€.");
		else if (num2 == 2)
			System.out.println("μ‚¬μš©μžκ°€ μ‘ŒμŠ΅λ‹ˆλ‹€..");
		else if (num2 == 3)
			System.out.println("μ‚¬μš©μžκ°€ μ΄κ²ΌμŠ΅λ‹ˆλ‹€..");
	}
	
	else if (num == 2) {
		System.out.println("μ‚¬μš©μž " + val[num] + " : 컴퓨터 " + val[num2]);
		if (num2 == 1)
			System.out.println("μ‚¬μš©μžκ°€ μ΄κ²ΌμŠ΅λ‹ˆλ‹€..");
		else if (num2 == 2)
			System.out.println("λΉ„κ²ΌμŠ΅λ‹ˆλ‹€.");
		else if (num2 == 3)
			System.out.println("μ‚¬μš©μžκ°€ μ‘ŒμŠ΅λ‹ˆλ‹€..");
	}
	
	else if (num == 3) {
		System.out.println("μ‚¬μš©μž " + val[num] + " : 컴퓨터 " + val[num2]);
		if (num2 == 1)
			System.out.println("μ‚¬μš©μžκ°€ μ‘ŒμŠ΅λ‹ˆλ‹€..");
		else if (num2 == 2)
			System.out.println("μ‚¬μš©μžκ°€ μ΄κ²ΌμŠ΅λ‹ˆλ‹€..");
		else if (num2 == 3)
			System.out.println("λΉ„κ²ΌμŠ΅λ‹ˆλ‹€.");
	}
	
	else {
		System.out.println("κ²Œμž„μ„ μ’…λ£Œν•©λ‹ˆλ‹€ ...");
		break;
	}

	}
	
	}
}

 

 

더 효율적인 λ‹€λ₯Έ 방법?이 μžˆμ„ 것 같은데 찾아봐야겠당..

7)

class Circle {
	private int x, y, radius;
	
    public Circle(int x, int y, int radius){
    	this.x = x;
    	this.y = y;
    	this.radius = radius;
    }
    
    public String toStirng(){
    	return "("+x+","+y+")"+"λ°˜μ§€λ¦„ "+radius;
    }
    
    
    public boolean equals(Circle c) { 
		if(radius == c.radius) 
			return true;
		else 
			return false;				
	}

    }
public class TEST {
	public static void main(String[] args) {
	Circle a = new Circle(1,2,10);
	Circle b = new Circle (5,6,10);
	System.out.println("원 1: " + a);
	System.out.println("원 2: " + b);
	if(a.equals(b)) System.out.println("같은 μ›μž…λ‹ˆλ‹€.");
	else System.out.println("λ‹€λ₯Έ μ›μž…λ‹ˆλ‹€.");
	}
}

 

 


bonus 1)

class Circle {
	private int x, y, radius;
	
    public Circle(int x, int y, int radius){
    	this.x = x;
    	this.y = y;
    	this.radius = radius;
    }
    
    public String toString(){ //객체λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•œλ‹€.
    	return "(" + x + "," + y + ")" + "λ°˜μ§€λ¦„ " + radius;
    }
    
    
    public boolean equals(Circle c) { 
		if(radius == c.radius) 
			return true;
		else 
			return false;				
	}

    }
public class TEST {
	public static void main(String [] args) {
	Circle a = new Circle(1,2,10);
	Circle b = new Circle (5,6,10);
	System.out.println("원 1: " + a);
	System.out.println("원 2: " + b);
	if(a.equals(b)) System.out.println("같은 μ›μž…λ‹ˆλ‹€.");
	else System.out.println("λ‹€λ₯Έ μ›μž…λ‹ˆλ‹€.");
	}
}

 

 


# bonus 2)

import java.util.Scanner;

public class TEST {
	public static void main(String [] args) {
	
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.print(">>");
			String s = sc.nextLine();
			StringBuffer sb = new StringBuffer(s);
			if (s.equals("exit")) {
				System.out.println("μ’…λ£Œν•©λ‹ˆλ‹€...");
				break;
			}
			int index =(int)(Math.random()*s.length());
			while (true) {
				int i = (int)(Math.random()*26);
				char c = (char)('a'+ i);
			if (!s.equals(c)) {
				sb.replace(index,index+1,Character.toString(c));
				break;
			}
			}
			System.out.println(sb);
		}
		
	}
}

StringButter μ‚¬μš©λ²•μ„ ν—·κ°ˆλ €μ„œ μ–΄λ €μ› λ‹€. (s) λ¬Έμžμ—΄μ„ 가진 객체둜 보면 될 것 κ°™λ‹€.

3)

import java.util.Scanner;

public class TEST {
	public static void main(String [] args) {
	
		System.out.println("λ¬Έμžμ—΄μ„ μž…λ ₯ν•˜μ„Έμš”. 빈칸, μ˜μ–΄, ν•œκΈ€ κ°€λŠ₯ν•©λ‹ˆλ‹€.");
		Scanner sc = new Scanner(System.in);
		String text =  sc.nextLine();
		
		System.out.println("<Enter>λ₯Ό μž…λ ₯ν•˜λ©΄ λ¬Έμžμ—΄μ΄ ν•œκΈ€μžμ”© νšŒμ „ν•©λ‹ˆλ‹€.");
		
		while (true) {
			String key = sc.nextLine();

			if (key.equals("")) {
				String first = Character.toString(text.charAt(0));
				String last = text.substring(1);
				text = last.concat(first);
				
				System.out.print(text + " >>");
			}
			
			else if (key.equals("q"))
				break;
			
			else
				System.out.print(text + " >>");
		}
		System.out.println("μ£΅λ£Œν•©λ‹ˆλ‹€...");
		sc.close();
		
	}
}