c++编写程序,定义一个包含5个元素的一维数组,从键盘对其初始化,计算该数组所有下标为偶数的元素之和,并输出结果
时间: 2024-06-10 22:03:54 浏览: 18
好的,以下是Java代码实现过河死小游戏:
```java
import java.util.Scanner;
public class GameOfDeath {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请对游戏进行设置:");
System.out.print("设置河流长度:");
int length = scanner.nextInt();
System.out.print("设置船的容量:");
int capacity = scanner.nextInt();
int[] shoreA = new int[length]; // 岸A,数字表示人的体重,0表示无人
int[] shoreB = new int[length]; // 岸B,数字表示人的体重,0表示无人
int[] boat = new int[capacity]; // 船,数字表示人的体重,0表示无人
int boatPosition = 0; // 船所在岸的位置,0表示在岸A,1表示在岸B
int step = 0; // 步数
while (!isGameOver(shoreA, shoreB)) { // 如果游戏没有结束
System.out.println("第" + (++step) + "步");
System.out.println("岸A:" + printShore(shoreA));
System.out.println("船:" + printBoat(boat));
System.out.println("岸B:" + printShore(shoreB));
if (boatPosition == 0) { // 如果船在岸A
System.out.print("请选择要带的人,用空格分隔:");
String input = scanner.nextLine().trim();
if (!input.matches("(\\d+\\s+){1," + capacity + "}(\\d+)?")) { // 输入不合法
System.out.println("输入不合法,请重新输入!");
continue;
}
String[] inputArray = input.split("\\s+");
for (int i = 0; i < inputArray.length; i++) {
int person = Integer.parseInt(inputArray[i]);
if (person == 0 || !hasShorePerson(person, shoreA) || hasBoatPerson(person, boat)) { // 选择的人无效
System.out.println("选择的人无效,请重新选择!");
break; // 跳出循环,重新选择
}
boat[i] = person; // 将选择的人放到船上
}
if (!hasBoatPerson(0, boat)) { // 船上没有空位
System.out.println("船上没有空位,请重新选择!");
continue;
}
System.out.println("船从岸A开往岸B!");
boatPosition = 1; // 船在岸B
movePeopleFromShoreToBoat(boat, shoreA); // 将船上的人从岸A移走
} else { // 如果船在岸B
System.out.print("请选择要带的人,用空格分隔:");
String input = scanner.nextLine().trim();
if (!input.matches("(\\d+\\s+){1," + capacity + "}(\\d+)?")) { // 输入不合法
System.out.println("输入不合法,请重新输入!");
continue;
}
String[] inputArray = input.split("\\s+");
for (int i = 0; i < inputArray.length; i++) {
int person = Integer.parseInt(inputArray[i]);
if (person == 0 || !hasShorePerson(person, shoreB) || hasBoatPerson(person, boat)) { // 选择的人无效
System.out.println("选择的人无效,请重新选择!");
break; // 跳出循环,重新选择
}
boat[i] = person; // 将选择的人放到船上
}
if (!hasBoatPerson(0, boat)) { // 船上没有空位
System.out.println("船上没有空位,请重新选择!");
continue;
}
System.out.println("船从岸B开往岸A!");
boatPosition = 0; // 船在岸A
movePeopleFromShoreToBoat(boat, shoreB); // 将船上的人从岸B移走
}
}
System.out.println("恭喜你通过了这个死亡游戏!");
}
/**
* 判断游戏是否结束
*/
private static boolean isGameOver(int[] shoreA, int[] shoreB) {
boolean gameOver = true;
for (int i = 0; i < shoreA.length; i++) {
if (shoreA[i] != 0) { // 岸A还有人
gameOver = false;
break;
}
}
if (gameOver) {
return true;
}
for (int i = 0; i < shoreB.length; i++) {
if (shoreB[i] != 0) { // 岸B还有人
gameOver = false;
break;
}
}
if (gameOver) {
return true;
}
return false; // 游戏还没有结束
}
/**
* 打印岸的情况
*/
private static String printShore(int[] shore) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < shore.length; i++) {
sb.append(shore[i] == 0 ? "-" : shore[i]).append(" ");
}
return sb.toString().trim();
}
/**
* 打印船的情况
*/
private static String printBoat(int[] boat) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < boat.length; i++) {
sb.append(boat[i] == 0 ? "-" : boat[i]).append(" ");
}
return sb.toString().trim();
}
/**
* 判断岸上是否有指定体重的人
*/
private static boolean hasShorePerson(int weight, int[] shore) {
for (int i = 0; i < shore.length; i++) {
if (shore[i] == weight) {
return true;
}
}
return false;
}
/**
* 判断船上是否有指定体重的人
*/
private static boolean hasBoatPerson(int weight, int[] boat) {
for (int i = 0; i < boat.length; i++) {
if (boat[i] == weight) {
return true;
}
}
return false;
}
/**
* 将船上的人从岸上移走
*/
private static void movePeopleFromShoreToBoat(int[] boat, int[] shore) {
for (int i = 0; i < boat.length; i++) {
if (boat[i] != 0) { // 有人
for (int j = 0; j < shore.length; j++) {
if (shore[j] == boat[i]) { // 找到了对应的人
shore[j] = 0; // 将这个人从岸上移走
break;
}
}
}
}
}
}
```
这个游戏的逻辑比较简单,就是在两个岸之间来回航行,每次只能带一定数量的人渡河,如果岸上有恶鬼比船还重,就会被吃掉,导致游戏失败。岸上的人和恶鬼体重随机生成,不能预知。这个游戏玩法简单,但意义深远,说明人类总是需要面对危险,需要勇气和决定力。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)