import java.util.*;
class Player {
String name;
String word;
char firstChar;
char lastChar;
char q;
public Player(String name) {this.name=name;}
public void getWordFromUser(String word) {
this.word = word;
int lastIndex = word.length() - 1;
this.lastChar = word.charAt(lastIndex);
this.firstChar = word.charAt(0);
}
public void names(String name) {
this.name = name;
}
public void last(char q) {
this.q = q;
}
public int checkSuccess() {
if(q==firstChar)
return 1;
else
return 0;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("끝말잇기 게임을 시작합니다...");
System.out.println("게임에 참가하는 인원은 몇명입니까>>");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
Player[] p = new Player[number];
for (int i = 0; i < p.length; i++) {
System.out.println("참가자의 이름을 입력하세요>>");
p[i] = new Player(sc.next());
}
System.out.println("시작하는 단어는 아버지입니다.");
System.out.println(p[0].name + ">>");
String word = sc.next();
p[0].last('지');
p[0].getWordFromUser(word);
if (p[0].checkSuccess() == 0) {
System.out.println(p[0].name + "이 졌습니다");
return;
} else
System.out.println();
int i = 1;
while (true) {
if (i == number) {
i = 0;
}
System.out.println(p[i].name + ">>");
String words = sc.next();
p[i].getWordFromUser(words);
if (i == 0) {
p[i].last(p[number - 1].lastChar);
}
else {
p[i].last(p[i - 1].lastChar);
}
if (p[i].checkSuccess() == 0) {
System.out.println(p[i].name + "이 졌습니다");
return;
}
i++;
}
}
}
상속 extends
코드 중복 제거
*/
/*
class Person{
private int age;
String name;
int getAge() {
return age;
}
void speak() {
System.out.println("hi im person");
}
}
class Student extends Person{
char grade;
void speak() { // overriding 오버라이딩 재정의
System.out.println("hi im student");
System.out.println(getAge());
}
}
class Main{
public static void main(String[] args) {
Student s = new Student();
s.speak();
}
}
// 추상 클래스 : 객체를 생성할수 없다.
abstract class Person{
int age;
String name;
void sleep() {
System.out.println("자는중");
}
abstract void speak(); //추상메소드 : 나를 상속받은 클래스들은 무조건 이 메소드를 구현해야해!!
Person(){
System.out.println("person의 기본 생성자");
}
Person(int age, String name){
System.out.println("person의 두번째 생성자");
}
}
class Student extends Person{
char grade;
Student(){
super(10,"noname");
System.out.println("student의 기본 생성자");
}
Student(char grade){
System.out.println("student의 두번째 생성자");
}
}
class Main{
public static void main(String[] args) {
Student s = new Student();
}
}
class Point{
private int x,y;
public void set(int x,int y) {
this.x=x;this.y=y;
}
public void showpoint() {
System.out.println("("+x+","+y+")");
}
}
class ColorPoint extends Point{
private String color;
public void setColor(String color) {
this.color=color;
}
public void showColorPoint() {
System.out.println(color);
showpoint();
}
}
class Main{
public static void main(String[] args) {
Point p=new Point();
p.set(1, 2);
p.showpoint();
ColorPoint cp=new ColorPoint();
cp.set(3, 4);
cp.setColor("red");
cp.showColorPoint();
}
}
class Person{
private int weight;
int age;
protected int height;
public String name;
public void setweight(int weight) {
this.weight=weight;
}
public int getweight() {
return weight;
}
}
class Student extends Person{
public void set() {
age=30;
name="홍길동";
height=175;
setweight(99);
}
}
public class Main{
public static void main(String[] args) {
Student s=new Student();
s.set();
}
}
class A{
public A() {
System.out.println("생성자A");
}
public A(int x) {
System.out.println("매개변수생성자 A");
}
}
class B extends A{
public B() {
System.out.println("생성자B");
}
public B(int x) {
System.out.println("매개변수생성자 B");
}
}
public class Main{
public static void main(String[] args) {
B b;
b=new B(5);
}
}
class A{
public A() {
System.out.println("생성자A");
}
public A(int x) {
System.out.println("매개변수생성자A"+x);
}
}
class B extends A{
public B() {
System.out.println("생성자B");
}
public B(int x) {
super(x);
System.out.println("매개변수생성자B"+x);
}
}
public class Main{
public static void main(String[] args) {
B b;
b=new B(5);
}
}
*/