[英]How can I make this Java program loop?
我是編程的新手,我需要添加一個JOptionPane,上面寫着:“再次播放?”。 選擇“是”和“否”。
如果選擇“是”,游戲將重新開始。 如果為“否”,游戲將終止。
任何幫助將非常感激。 提前致謝!
import javax.swing.JOptionPane;
import java.util.Random;
public class JackAndPoy {
public static void main(String[] args)
{
String computerSide,
userSide;
JOptionPane.showMessageDialog(null, " Play the Game \"Rock, Paper, Scissors?\"");
computerSide = ComputerChoice();
userSide = UserChoice();
if (userSide != null)
{
JOptionPane.showMessageDialog(null, "The computer's choice is " + computerSide + ".");
Winner(computerSide, userSide);
}
else {
JOptionPane.showMessageDialog(null, "Error: Improper UserEntry. Please enter either" +
" 'rock', 'paper', or'scissors'.");
}
}
public static String ComputerChoice()
{
byte computerChoice;
String computerChoiceString = "";
Random choiceGenerator = new Random();
computerChoice = (byte)(choiceGenerator.nextInt(3) + 1);
switch (computerChoice)
{
case 1:
{
computerChoiceString = "rock";
break;
}
case 2:
{
computerChoiceString = "paper";
break;
}
case 3:
{
computerChoiceString = "scissors";
break;
}
}
return computerChoiceString;
}
public static String UserChoice()
{
String userChoice,
userChoiceLowerCase;
userChoice =
JOptionPane.showInputDialog("Choice: [rock][paper][scissors]");
if (userChoice.equalsIgnoreCase("rock") || userChoice.equalsIgnoreCase("paper")
|| userChoice.equalsIgnoreCase("scissors"))
{
userChoiceLowerCase = userChoice.toLowerCase();
}
else {
userChoiceLowerCase = null;
}
return userChoiceLowerCase;
}
public static void Winner(String computerSide, String userSide)
{
if (computerSide.equals(userSide)) {
JOptionPane.showMessageDialog(null, "The game has to be played again, because we have a tie.");
}
else if (computerSide.equalsIgnoreCase("rock") && userSide.equalsIgnoreCase("paper")) {
JOptionPane.showMessageDialog(null, "You win. Paper covers rock.");
}
else if (computerSide.equalsIgnoreCase("rock") && userSide.equalsIgnoreCase("scissors")) {
JOptionPane.showMessageDialog(null, "You lose. Rock crushes scissors.");
}
else if (computerSide.equalsIgnoreCase("paper") && userSide.equalsIgnoreCase("rock")) {
JOptionPane.showMessageDialog(null, "You lose. Paper covers rock.");
}
else if (computerSide.equalsIgnoreCase("paper") && userSide.equalsIgnoreCase("scissors")) {
JOptionPane.showMessageDialog(null, "You win. Scissors cuts paper.");
}
else if (computerSide.equalsIgnoreCase("scissors") && userSide.equalsIgnoreCase("rock")) {
JOptionPane.showMessageDialog(null, "You win. Rock crushes scissors.");
}
else {
JOptionPane.showMessageDialog(null, "You lose. Scissors cuts paper.");
}
}
}
您需要一個do-while循環,該循環執行一個動作,然后檢查條件以再次執行該動作。 這與常規while循環有所不同。 do-while循環必須至少執行一次該代碼塊。 因此,像這樣更改您的主要方法:
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,
" Play the Game \"Rock, Paper, Scissors?\"");
String computerSide, userSide;
do
{
computerSide = ComputerChoice();
userSide = UserChoice();
if(userSide != null)
{
JOptionPane.showMessageDialog(null, "The computer's choice is "
+ computerSide + ".");
Winner(computerSide, userSide);
}
else
{
JOptionPane.showMessageDialog(null,
"Error: Improper UserEntry. Please enter either"
+ " 'rock', 'paper', or'scissors'.");
}
}
while(JOptionPane.showConfirmDialog(null, "Play Again?", "Rock-Paper-Scissors", JOptionPane.YES_NO_OPTION) == 0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.