其他分享
首页 > 其他分享> > 小游戏—选队长

小游戏—选队长

作者:互联网

选队长游戏

所有人围成一圈,顺序排号。从第一个人开始报数(从 1 到 3 报数),

凡报到 3 的人退出圈子,剩下的人继续报数,最后留下的当选为队长。

import java.util.Scanner;

public class SelectCaptain {

	//总人数
	private int count;
	//数组
	private int[] persons;
	//输入人数
	public int input() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入爬山的人数:");
		if(input.hasNextInt()) {
			this.count = input.nextInt();
		}else {
			System.out.println("请输入数字!");
			input();
		}
		//创建数组
		createArray(count);
		//开始游戏
		startGame();
		return this.count;
	}
	
	public int[] createArray(int count) {
		this.persons = new int[count];
		for(int i = 0;i<this.persons.length;i++) {
			persons[i] = i+1;
		}
		System.out.println("参加人员:");
		for(int i = 0 ;i < this.persons.length;i++) {
			System.out.print(this.persons[i]+"\t");
		}
		System.out.println();
		return this.persons;
	}
	
	//开始游戏
	public void startGame() {
		//计数
		int countAmount = 0;
		//只要人数还剩下一个人
		int left = this.count;
		System.out.println("出局顺序:");
		while (left > 1) {
			for(int i = 0;i < this.persons.length;i++) {
				if(this.persons[i] != 0) {
					countAmount++;
					if(countAmount == 3) {
						System.out.print(this.persons[i]+"\t");
						countAmount = 0;
						this.persons[i] = 0;//落选
						left--;
					}
				}
			}
		}
		System.out.println();
		for(int i = 0;i<this.persons.length;i++) {
			if(this.persons[i] != 0) {
				System.out.println("队长是:" + this.persons[i]);
			}
		}
		
	}
	
	
}

运行结果:image-20211002215401871

标签:队长,count,int,System,persons,小游戏,input,out
来源: https://www.cnblogs.com/mxkgmn/p/15362821.html