質問失礼します。今どちらかじゃんけんで三勝したらグリコの配点で勝敗を決めるゲームを作っています。しかし下記のコードを書いてみたところwhileを入れた瞬間急に動かなくなったり、明らかに乱数ではないもの(コンピューターの手)が出てしまったりと不具合が起きてしまいます。自分なりに原因を考えてみたところ全く分からない状況です。どなたかご教授いただけないでしょうか。
import javax.swing.JFrame; public class GameMain { public static void main(String[] args) { // TODO Auto-generated method stub //ウィンドウ設定 JFrame frame = new JFrame("Guriko"); frame.setSize(640, 480); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); Panel.createPanel(frame); //ウィンドウ表示 frame.setVisible(true); } }
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Panel { public static JLabel headerLabel; public static JLabel contentsLabel; public static void createPanel(JFrame frame) { //ヘッダーパネル Dimension headerPanelDimension = new Dimension(640, 50); JPanel headerPanel = setPanel(Color.BLACK, headerPanelDimension); headerLabel = new JLabel("Let's play Guriko"); headerLabel = setFont(Color.WHITE,headerLabel,24); headerPanel.add(headerLabel); frame.add(headerPanel,BorderLayout.NORTH); //コンテンツパネル Dimension contentsPanelDimension = new Dimension(640, 380); JPanel contentsPanel = setPanel(Color.WHITE,contentsPanelDimension); contentsLabel = new JLabel("Rock-Scissors-Paper..."); contentsLabel = setFont(Color.BLACK,contentsLabel,54); contentsPanel.add(contentsLabel); frame.add(contentsPanel,BorderLayout.CENTER); //フッタパネル Dimension footerPanelDimension = new Dimension(640, 50); JPanel footerPanel = setPanel(Color.BLACK,footerPanelDimension); Player.createButton(footerPanel); frame.add(footerPanel,BorderLayout.SOUTH); } public static JPanel setPanel(Color color, Dimension PanelDimension) { JPanel panel = new JPanel(); panel.setPreferredSize(PanelDimension); panel.setLayout(new BorderLayout()); panel.setBackground(color); return(panel); } public static JLabel setFont(Color clr,JLabel label,int strSize) { label.setForeground(clr); Font labelFont = new Font("MS ゴシック",Font.PLAIN,strSize); label.setFont(labelFont); label.setHorizontalAlignment(JLabel.CENTER); label.setVerticalAlignment(JLabel.CENTER); return(label); } }
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JPanel; public class Player implements ActionListener { int win, lose, points1,points2; public static void createButton(JPanel footerPanel) { //ボタンを表示 JButton btnGu = new JButton("Rock"); btnGu = setButton(btnGu); footerPanel.add(btnGu, BorderLayout.WEST); JButton btnChoki = new JButton("Scissors"); btnChoki = setButton(btnChoki); footerPanel.add(btnChoki, BorderLayout.CENTER); JButton btnPa = new JButton("Paper"); btnPa = setButton(btnPa); footerPanel.add(btnPa, BorderLayout.EAST); } public static JButton setButton(JButton button) { int buttonSizeX = (640 - 20)/3; Dimension buttonDimesion = new Dimension(buttonSizeX, 50); button.setPreferredSize(buttonDimesion); Font buttonFont = new Font("MS ゴシック",Font.PLAIN,24); button.setFont(buttonFont); Player player = new Player(); button.addActionListener(player); return(button); } public void actionPerformed(ActionEvent e) { win = 0; lose = 0; while (win < 3 && lose < 3) { String command = e.getActionCommand(); int playerHand = 0; if (command.equals("Rock")) playerHand = 0; else if (command.equals("Scissors")) playerHand = 1; else if (command.equals("Paper")) playerHand = 2; Random random = new Random(); int computerHand = random.nextInt(3); if (computerHand == 0) Panel.contentsLabel.setText("Rock"); else if (computerHand == 1) Panel.contentsLabel.setText("Scissors"); else if (computerHand == 2) Panel.contentsLabel.setText("Paper"); if(playerHand==0 && computerHand==1) { Panel.headerLabel.setText("Win"); win++; } else if(playerHand==1 && computerHand==2 || playerHand==2 && computerHand==0) { Panel.headerLabel.setText("Win"); win++; } else if(playerHand==0 && computerHand==2 || playerHand==2 && computerHand==1) { Panel.headerLabel.setText("Lose"); lose++; } else if(playerHand==1 && computerHand==0) { Panel.headerLabel.setText("Lose"); lose++; } else if(playerHand==computerHand) { Panel.headerLabel.setText("Drew"); } } } }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。