前提・実現したいこと
Java初心者です。思い通りの結果表示にならず困っています。
アドバイスいただけたらとても助かります。
仕様はJavaで、GuiのSwingで実行結果を表示します。
作成プログラム:じゃんけん
★作成したい内容
じゃんけんを10回行う
↓
じゃんけんを終了
↓
結果を表示(←ここは、まだコード作成していません。問題解決していない為。)
発生している問題・エラーメッセージ
★解決したいところ
11回目以降~もSwingで設定したボタンを押し続けると、じゃんけんが無限に続いてしまう。
10回じゃんけんを行ったら、結果を表示する作業に移りたいです。
該当のソースコード
java
1 2import java.awt.Color; 3import java.awt.Font; 4import java.awt.event.ActionEvent; 5import java.awt.event.ActionListener; 6import java.util.Random; 7 8import javax.swing.ImageIcon; 9import javax.swing.JButton; 10import javax.swing.JFrame; 11import javax.swing.JLabel; 12import javax.swing.JPanel; 13 14public class Janken extends JFrame implements ActionListener { 15 private static final long serialVersionUID = 1L; 16 JButton rock; 17 JButton scissors; 18 JButton paper; 19 JLabel label; 20 JLabel image; 21 JLabel image2; 22 int countNum; 23 24 public Janken(String title) { 25 super(title); 26 setBounds(700, 700, 700, 700); 27 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 JPanel p = new JPanel(); 29 p.setLayout(null); 30 label = new JLabel("サザエさんと、じゃんけんしましょ", JLabel.CENTER); 31 label.setFont(new Font(null, Font.BOLD, 30)); 32 image = new JLabel( 33 new ImageIcon("C:\amslssn\pleiades\workspace\Lesson\src\javaHumanNet\ダウンロード (1).jpg")); 34 image.setBounds(150, 0, 400, 400); 35 add(image); 36 add("North", label); 37 rock = new JButton(new ImageIcon("rock.gif")); 38 rock.setBounds(150, 400, 100, 100); 39 rock.addActionListener(this); 40 p.add(rock); 41 scissors = new JButton(new ImageIcon("scissors.gif")); 42 scissors.setBounds(300, 400, 100, 100); 43 scissors.addActionListener(this); 44 p.add(scissors); 45 paper = new JButton(new ImageIcon("paper.gif")); 46 paper.setBounds(450, 400, 100, 100); 47 paper.addActionListener(this); 48 p.add(paper); 49 p.setBackground(Color.gray); 50 add("Center", p); 51 setVisible(true); 52 countNum = 0; 53 } 54 55 public static void main(String[] args) { 56 Janken jk = new Janken("じゃんけんゲーム"); 57 while(true){ 58 try { 59 Thread.sleep(100); 60 } catch (InterruptedException e) { 61 // TODO 自動生成された catch ブロック 62 e.printStackTrace(); 63 } 64 if(jk.countNum > 10){ 65 System.out.println("end"); 66 67 break; 68 } 69 } 70 } 71 72 public void actionPerformed(ActionEvent e) { 73 int iYou = 0; 74 if (e.getSource() == scissors) { 75 iYou = 1; 76 } else if (e.getSource() == paper) { 77 iYou = 2; 78 } 79 Random r = new Random(); 80 int j = r.nextInt(3); 81 String[] Hand = { "グー", "チョキ", "パー" }; 82 83 84 if (j == iYou) { 85 label.setText("サザエさんは" + Hand[j] + "、引き分けです"); 86 } else if ((iYou == 0 && j == 1) || (iYou == 1 && j == 2) || (iYou == 2 && j == 0)) { 87 label.setText("サザエさんは" + Hand[j] + "、あなたの勝ちです"); 88 } else { 89 label.setText("サザエさんは" + Hand[j] + "、サザエさんの勝ちです"); 90 } 91 countNum++; 92 93 94 } 95} 96 97 98### 試したこと 99mainメソッドにwhile文を使用し、回数制限を試みましたが上手くいきませんでした。 100 101よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー