私はStone、Sheet、Shearsゲームのプログラムを作ることになっています。これには、Math.random(). から来る人間の遊びとコンピューターの遊びが含まれます。ということで、4が入るまで無限大にすることにしました。
仕組みは次のとおりです。
遊びたい?PCに勝て!
- 結石
- シート
- 鋏
- 終了する
4 を入力すると終了しますが、 1 、 2 、または 3 が入力されている限り継続的に実行する必要があります。どうやってやるの。これが私の現在のコードです。while player != 4 の配置が間違っているのはわかっています
import javax.swing.*;
public class StoneSheetShears
{
public static void main(String[] args)
{
String consoleStr;
int player = 0;
int[] numChoices = {1,2,3,4};
String[] strChoices = {"Stone", "Sheet", "Shears", "Quit"};
String playerChoice = "";
String compChoice = "";
int computer = (int)(Math.random() * 3);
String output = "";
while(true)
{
do
{
try
{
consoleStr = JOptionPane.showInputDialog("Beat the computer\n1. Rock\n2. Paper" +
"\n3. Scissors\n4. Quit ");
player = Integer.parseInt(consoleStr);
for(int x = 0; x <numChoices.length; x++)
{
if(player == numChoices[x])
{
playerChoice = strChoices[x];
}
}
for(int y = 0; y <numChoices.length; y++)
{
if(computer == numChoices[y])
{
compChoice = strChoices[y];
}
}
}
}while(player!=4)
catch (NumberFormatException err)
{
JOptionPane.showMessageDialog(null, "There is an error on entry",
"Error Message", JOptionPane.WARNING_MESSAGE);
continue;
}
break;
}
if (player == computer)
{
output = "Both are " + compChoice;
JOptionPane.showMessageDialog(null, output, "DRAW!", JOptionPane.INFORMATION_MESSAGE);
}
else if (player == 1)
{
if (computer == 2)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
}
else if (computer == 3)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
else if (player == 2)
{
if (computer == 3)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
}
else if (computer == 1)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
else if (player == 3)
{
if (computer == 1)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
}
else if (computer == 2)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
基本的に石板と鋏を選択するゲームです。
- 人間のプレイヤーは、1 (石)、2 (シート)、3 (ハサミ)、4 (やめる) から選択します。
- コンピュータ プレーヤーは、数学ランダムに基づいています。
配列を使用して、人間のプレーヤーとコンピューターの動きを定義しました。(配列は Java 構文の一部であり、このような説明には適していません。) - 両方とも同じ動きだったら引き分けです。
- 人間が紙を選び、コンピューターのプレーヤーがはさみなら、明らかにコンピューターが勝ちます。同じことが他の選択肢にも当てはまります。
プレーヤーは、1 2 または 3 のいずれかの別の数字を入力します。(これは単に手順 1 を繰り返すだけなので、不要です)。4 を入力するとゲームが停止します。これは、プロンプトに示されているように終了することを意味します。
私の唯一の問題は、4 が入力されるまで継続的に動作させるにはどうすればよいかということです。