"κ°μ ν: λͺ
ν 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();
}
}