我有一个模拟两个玩家掷硬币并猜测头或尾的游戏。当第一个玩家获得5分时,游戏假设停止。我已经尝试了一段时间的声明,但它不会停止,直到两者都超过5。编程新手并且可以使用比我更多知识的新眼睛。谢谢大家的帮助。 (看看从//开始游戏开始的代码。)
/**
* This program demonstrates the coin class
* and plays a game between two players.
*/
public class CoinGame
{
public static void main(String[] args)
{
final int maxPoints = 5; // Number of rounds
String player1Name; // First player's name
String player2Name; // Second player's name
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the player's names.
System.out.print("Enter the first player's name: ");
player1Name = keyboard.nextLine();
System.out.print("Enter the second player's name: ");
player2Name = keyboard.nextLine();
// Create the coin.
Coin coin = new Coin();
// Create the two players.
Player player1 = new Player(player1Name);
Player player2 = new Player(player2Name);
// Play the game.
while (player1.getPoints() <= maxPoints || player2.getPoints() <= maxPoints)
{
System.out.println("----------------------------");
// System.out.printf("Now playing round %d.\n", round + 1);
// Flip the coin.
coin.toss();
// The players make their guesses.
player1.makeGuess();
player2.makeGuess();
// Determine the winner of this round.
roundResults(coin, player1, player2);
}
// Display the grand winner.
displayGrandWinner(player1, player2);
}
/**
* The roundResults method determines the results of
* the current round. The parameters are:
* coin: The Coin object
* player1: Player #1 object
* player2: Player #2 object
*/
public static void roundResults(Coin coin, Player player1,
Player player2)
{
// Show the coin side.
/*System.out.printf("The coin turned up %d.\n",
coin.getSideUp());
*/
System.out.printf("The coin turned up: %s\n", coin.getSideUp());
// Check each player's guess and award points.
checkGuess(player1, coin);
checkGuess(player2, coin);
}
/**
* The checkGuess method checks a player's guess against
* the coin's result. The parameters are:
* player: The Player object to check.
* coin: The Coin object.
*/
public static void checkGuess(Player player, Coin coin)
{
final int POINTS_TO_ADD = 1; // Points to award winner
String guess = player.getGuess(); // Player's guess
String coinResult = coin.getSideUp(); // Heads or Tails
// Display the player's guess.
System.out.printf("The player %s guessed %s.\n",
player.getName(), player.getGuess());
// Award points if the player guessed correctly.
if (guess.equalsIgnoreCase(coinResult))
{
player.addPoints(POINTS_TO_ADD);
System.out.printf("Awarding %d point(s) to %s.\n",
POINTS_TO_ADD, player.getName());
}
}
/**
* The displayGrandWinner method displays the game's grand winner.
* The parameters are:
* player1: Player #1
* player2: Player #2
*/
public static void displayGrandWinner(Player player1, Player player2)
{
System.out.println("----------------------------");
System.out.println("Game over. Here are the results:");
System.out.printf("%s: %d points.\n", player1.getName(),
player1.getPoints());
System.out.printf("%s: %d points.\n", player2.getName(),
player2.getPoints());
if (player1.getPoints() > player2.getPoints())
System.out.println(player1.getName() + " is the grand winner!");
else if (player2.getPoints() > player1.getPoints())
System.out.println(player2.getName() + " is the grand winner!");
else
System.out.println("Both players are tied!");
}
}
答案 0 :(得分:3)
更改
while (player1.getPoints() <= maxPoints || player2.getPoints() <= maxPoints)
到
while (player1.getPoints() <= maxPoints && player2.getPoints() <= maxPoints)
答案 1 :(得分:0)
while循环:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.charts.load('current', {'packages': ['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getData.php",
dataType: "json",
async: false
}).responseText;
jsonData = JSON.parse(jsonData);
jsonData = jsonData.channels[2].values;
var dataArray = [['ts', 'values']];
for (var i = 0; i < jsonData.length; i++)
{
var tempArray = [jsonData[i].ts, parseFloat(jsonData[i].value.replace(",", "."))];
dataArray.push(tempArray);
}
// Create our data table out of JSON data loaded from server.
var data = google.visualization.arrayToDataTable(
dataArray
);
var options = {
hAxis: {title: 'TIME-> "TS" Variable', titleTextStyle: {color: '#333'}},
vAxis: {title: 'Values', titleTextStyle: {color: '#333'}}
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div" style="width: 100%; height: 500px;"></div>
</body>
</html>
应改为:
while (player1.getPoints() <= maxPoints || player2.getPoints() <= maxPoints)
原因是while (player1.getPoints() <= maxPoints && player2.getPoints() <= maxPoints)
意味着while循环不会退出,除非BOTH玩家大于5,而不是一个。将其更改为||
意味着仅在两者都为真时执行循环。