반응형
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' 카테고리의 다른 글
자바 프로그래밍 이클립스 가위바위보 게임 프로그래밍 소스 코드 (0) | 2023.02.18 |
---|---|
자바프로그래밍(JAVA) 번호 맞추기 게임(Up & Down) (0) | 2023.02.11 |
JAVA프로그래밍 Scanner 클래스 정수 입력받아 합 출력하기 (0) | 2023.01.28 |
자바(JAVA)프로그래밍 점수 입력 받아 등급 출력하기 (0) | 2023.01.21 |
자바(JAVA) 반복문 for문 사용법 구구단 소스코드 (0) | 2022.12.08 |
댓글