gpt4 book ai didi

Java 程序陷入用户输入循环

转载 作者:行者123 更新时间:2023-12-01 22:29:17 27 4
gpt4 key购买 nike

我正在创建一个小型“游戏”程序,其中玩家输入楼层/房间号,但是当玩家猜测时,它会卡住并在单个玩家上循环,并且不会移动到下一个玩家,也不会判断玩家对狗被关在建筑物中的位置的猜测是否正确。

PuppyPlay.java:

import java.util.Random;
import java.util.Scanner;

/**
* This program is used as a driver program to play the game from the
* class LostPuppy.
*
* A puppy is lost in a multi-floor building represented in the class
* LostPuppy.class. Two players will take turns searching the building
* by selecting a floor and a room where the puppy might be.
*
*/

public class PuppyPlay{
/**
* Driver program to play LostPuppy.
*
* @param theArgs may contain file names in an array of type String
*/
public static void main(String[] theArgs){
Scanner s = new Scanner(System.in);
LostPuppy game;
int totalFloors;
int totalRooms;
int floor;
int room;
char[] players = {'1', '2'};
int playerIndex;
boolean found = false;
Random rand = new Random();

do {
System.out.print("To find the puppy, we need to know:\n"
+ "\tHow many floors are in the building\n"
+ "\tHow many rooms are on the floors\n\n"
+ " Please enter the number of floors: ");
totalFloors = s.nextInt();
System.out.print("Please enter the number of rooms on the floors: ");
totalRooms = s.nextInt();
s.nextLine(); // Consume previous newline character

// Start the game: Create a LostPuppy object:
game = new LostPuppy(totalFloors, totalRooms);

// Pick starting player
playerIndex = rand.nextInt(2);

System.out.println("\nFloor and room numbers start at zero '0'");

do {

do {
System.out.println("\nPlayer " + players[playerIndex]
+ ", enter floor and room to search separated by a space: ");
floor = s.nextInt();
room = s.nextInt();

//for testing, use random generation of floor and room
//floor = rand.nextInt(totalFloors);
//room = rand.nextInt(totalRooms);
} while (!game.indicesOK(floor, room)
|| game.roomSearchedAlready(floor, room));


found = game.searchRoom(floor, room, players[playerIndex]);
playerIndex = (playerIndex + 1) % 2;
System.out.println("\n[" + floor + "], [" + room + "]");
System.out.println(game.toString());
s.nextLine();
} while (!found);

playerIndex = (playerIndex + 1) % 2;
System.out.println("Great job player " + players[playerIndex] +"!");
System.out.println("Would you like to find another puppy [Y/N]? ");
} while (s.nextLine().equalsIgnoreCase("Y"));
}
}

LostPuppy.java:

import java.util.Random; // Randomize the dog placement in building
import java.util.Scanner; // User input

/**
* This program is used as a program to play the game from the
* driver PuppyPlay.java
*
* A puppy is lost in a multi-floor building represented in the class
* LostPuppy.class. Two players will take turns searching the building
* by selecting a floor and a room where the puppy might be.
*
*/

public class LostPuppy{

private char[][] myHidingPlaces; // Defining class fields for assignment
private int myFloorLocation;
private int myRoomLocation;
private char myWinner;
private boolean myFound;

/**
* Creates constructor takes floor/room numbers inputted by user
*
* @param theFloors for number of floors
* @param theRooms for number of rooms
*/

public LostPuppy(int theFloors, int theRooms) {
Random random = new Random();
myHidingPlaces = new char[theFloors][theRooms];

// Filling array with spaces
int i;
for (i = 0; i < theFloors; i++) {
for (int k = 0; k < theRooms; k++) {
myHidingPlaces[i][k] = ' ';
}
}

myFloorLocation = random.nextInt(theFloors);
myRoomLocation = random.nextInt(theRooms);
myHidingPlaces[myFloorLocation][myRoomLocation] = 'P';
myWinner = ' ';
myFound = false;
}

/**
* Checks if room has been searched prior
*
* @param theFloors for number of floors
* @param theRooms for number of rooms
*/

public boolean roomSearchedAlready(int theFloors, int theRooms) {
boolean searchedRoom;
if (myHidingPlaces[theFloors][theRooms] == ' ') {
myHidingPlaces[theFloors][theRooms] = 'S';
searchedRoom = false;
} else {
searchedRoom = true;
}
return searchedRoom;
}

/**
* Checks if the puppy has been found
*
* @param theFloors for number of floors
* @param theRooms for number of rooms
*/

public boolean puppyLocation(int theFloors, int theRooms) {
if (myHidingPlaces[myFloorLocation][myRoomLocation] == myHidingPlaces[theFloors][theRooms]) {
myFound = true;
} else {
myFound = false;
}
return myFound;
}

/**
* Checks if floors and rooms won't throw out of bounds error
*
* @param theFloors for number of floors
* @param theRooms for number of rooms
*/

public boolean indicesOK(int theFloors, int theRooms) {
boolean indicesFit;
if (theFloors < numberOfFloors() && theRooms < numberOfRooms()) {
indicesFit = true;
} else {
indicesFit = false;
}
return indicesFit;
}

/*
* Checks # of floors and returns it
*/

public int numberOfFloors() {
return myHidingPlaces.length;
}

/*
* Checks # of rooms and returns it
*/

public int numberOfRooms() {
return myHidingPlaces[0].length;
}

/**
* Checks which player found the dog and won, or if not checks to see what player
* guessed wrong and puts their # in the box
*
* @param theFloors for number of floors
* @param theRooms for number of rooms
* @param thePlayer for 1st or 2nd player
*/

public boolean searchRoom(int theFloors, int theRooms, char thePlayer) {
if (myHidingPlaces[myFloorLocation][myRoomLocation] == myHidingPlaces[theFloors][theRooms]) {
myFound = true;
myWinner = thePlayer;
} else {
myHidingPlaces[theFloors][theRooms] = thePlayer;
myFound = false;
}
return myFound;
}

/*
* toString displays the current hidingPlaces array and it’s contents EXCEPT
* the location of the puppy which remains hidden until he/she is found at
* which point toString will be called (by the driver) and both the player
* who found the puppy and a ‘P’ will be displayed in the same cell….
*
*
*
*/

public String toString() {
return null;
}
}

要运行此代码,您需要将两个代码及其各自的发布名称放在一起,并在同一文件夹中运行 PuppyPlay.java 和 LostPuppy.java(仅供引用)。

最佳答案

问题出在PuppyPlay的这个地方:

    found = game.searchRoom(floor, room, players[playerIndex]);
playerIndex = (playerIndex + 1) % 2;
System.out.println("\n[" + floor + "], [" + room + "]");
System.out.println(game.toString());
s.nextLine();

所以你的程序希望你在这里输入一些东西,它会一直等待,直到你按下回车键,所以你可以删除该行:s.nextLine();

关于Java 程序陷入用户输入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28144902/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com