본문 바로가기
프로그래밍언어/Java

자바 프로그래밍 이클립스 끝말잇기 게임 소스 코드

by 보안의신 2023. 2. 4.
반응형
import java.util.Scanner;

public class WordGameApp {

public static void main(String[] args) {

	// TODO Auto-generated method stub

	Scanner s = new Scanner(System.in);

	System.out.print("게임에 참가하는 인원은 몇명입니까?>>");

	int num = s.nextInt();

	Player player[] = new Player[num];

	for(int i = 0; i<num; i++){

		System.out.print("참가자의 이름을 입력하세요>>");

		String name = s.next();

		player[i] = new Player(name);

	}

	System.out.println("시작하는 단어는 아버지입니다");

	int i = 0;

	while(true){

		if(i==num){

		i = 0;

	}

	System.out.print(player[i].name+">>");

	String word = s.next();

	player[i++].sayWord(word);

	}

	}


}

class Player{

	//사용자 이름

	String name;

	static String word = "아버지";

	int lastIndex;

	char lastChar;

	char firstChar;

	//사용자로부터 입력받는 단어를 처리하는 매소드

	public void sayWord(String w){

	this.lastIndex = word.length() -1;

	this.lastChar = word.charAt(lastIndex);

	this.firstChar = w.charAt(0);

	if(lastChar!=firstChar){ // 첫글자와 끝글자가 다르다면 프로그램 종료

		System.out.println(this.name +"이 졌습니다.");

		System.exit(0);

	}

	this.word = w;

}

	public Player(String n){

	this.name = n;

	}

}

 

 

콘솔 결과창

java

 

반응형

댓글