我目前正在制作Tic Tac Toe游戏。当一名球员获胜并被问及是否想再次参赛时,我会调用Playing();
方法重新开始比赛。我遇到的问题是它会在第一个Playing();
停止Scanner
而不停止。
如何让循环停在第一个Scanner
(要求用户输入他们的[row]
?
Output
:
Enter [Row] : Please enter a number :
Enter [Row] :
Playing()
方法
public static void Playing() {
String line;
// Check if the user's input is a number. If not, retry!
while (true) {
try {
System.out.print("\nEnter [Row] : "); //Tell user to input Row
line = input.nextLine();
row = Integer.parseInt(line) - 1;
System.out.print("Enter [Col] : "); //Tell user to input Col
line = input.nextLine();
col = Integer.parseInt(line) - 1;
break;
} catch (NumberFormatException e) {
System.out.print("Please enter a number : ");
break;
}
}
//Check if their input for [row][col] is valid
if (row < 0 || row > 2 || col < 0 || col > 2) {
System.out.print("Oops! Invalid input. Try again");
Playing();
} else if (Board[row][col] != '_') {
System.out.print("This position is already taken. Try again");
Playing();
} else {
Board[row][col] = player;
moveCount++;
DisplayBoard();
GameOver(); //Check if anyone won. If not, continue the game
}
}
Replay()
方法
public static void Replay() {
/*
* Check if the user wants to play again
*/
System.out.print("Would you like to play again?(Y or N) : ");
while (true) {
String retry = input.next();
if ("y".equalsIgnoreCase(retry)) {
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
Board[r][c] = '_';
}
}
System.out.print("-----------------------------------------------------\n");
DisplayBoard();
Playing();
break;
} else if ("n".equalsIgnoreCase(retry)) {
System.out.println("Thank you for Playing!");
System.exit(1);
// If the user enters an invalid input, this will ask them to try again
} else {
System.out.print("Invalid input. Would you like to play again?(Y or N) : ");
}
}
}
答案 0 :(得分:1)
首先,使用
的原因是什么<?php
$servername = "";
$user = "";
$pw = "";
$dbname = "";
$conn = new mysqli($servername, $user, $pw, $dbname);
if ($conn->connect_error) {
die("Could not connect to the server.");
}
$someone = $_GET["user"];
$sql = "SELECT * FROM some_table WHERE some_row='$someone'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Welcome, " . $row["some_row"]. "<span id='date'></span>
<script>
var date = new Date();
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
document.getElementById('date').innerHTML = days[date.getDay()];
</script>
";
}
} else {
echo "Error";
}
?>
其中一个人可以使用Scanner提供的nextInt()方法实际获取整数
line = input.nextLine();
row = Integer.parseInt(line) - 1;
为了回答你的问题,我认为这是导致跳过你的输入的罪魁祸首
line = input.nextInt();
row = line - 1;
如果您输入一些关键字,请在此行中输入&#34; Hello&#34;然后点击输入&#34; Hello \ n&#34;下一个方法只需要&#34; Hello&#34;并且\ n将跳过你的nextLine()方法。所以我建议你尝试在这一行之后添加另一个nextLine()
String retry = input.next();
注意:这只是一个问题的猜测,我还没有通过在我的终端系统上运行来调试你的代码。