質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

解決済

1回答

4476閲覧

二択問題の作り方について質問です。

Hanamarunanngo

総合スコア11

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

1グッド

1クリップ

投稿2017/08/16 12:42

編集2017/08/16 12:54

以下の二択問題のプログラムについて質問です。
これは、swingを用いた二択問題です。将来的には四択問題にするつもりです。
プログラミング初心者なため、とても回りくどい書き方ですみません。
これのプログラムをもっと簡潔に書きたいのですが、どのようにすればいいのか分かりません。
何かアドバイスをください。よろしくお願いします。

import javax.swing.*; import java.awt.CardLayout; import java.awt.BorderLayout; import java.awt.event.*; public class sample extends JFrame implements ActionListener{ JPanel cardPanel; CardLayout layout; int n = 0; public static void main(String[] args){ sample frame = new sample(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 300, 200); frame.setTitle("タイトル"); frame.setVisible(true); } sample(){ /* 問題カード1 */ JPanel card1 = new JPanel(); card1.add(new JLabel("質問1")); JButton btn1 = new JButton("btn1"); card1.add(btn1); JButton btn2 = new JButton("btn2"); card1.add(btn2); /* 問題カード2 */ JPanel card2 = new JPanel(); card2.add(new JLabel("質問2")); JButton btn3 = new JButton("btn3"); card2.add(btn3); JButton btn4 = new JButton("btn4"); card2.add(btn4); /* 問題カード3 */ JPanel card3 = new JPanel(); card3.add(new JLabel("質問3")); JButton btn5 = new JButton("btn5"); card3.add(btn5); JButton btn6 = new JButton("btn6"); card3.add(btn6); /* ボタンアクション*/ btn1.addActionListener(this); btn1.setActionCommand("btn1"); btn2.addActionListener(this); btn2.setActionCommand("btn2"); btn3.addActionListener(this); btn3.setActionCommand("btn3"); btn4.addActionListener(this); btn4.setActionCommand("btn4"); btn5.addActionListener(this); btn5.setActionCommand("btn5"); btn6.addActionListener(this); btn6.setActionCommand("btn6"); cardPanel = new JPanel(); layout = new CardLayout(); cardPanel.setLayout(layout); cardPanel.add(card1, "btn1"); cardPanel.add(card1, "btn2"); cardPanel.add(card2, "btn3"); cardPanel.add(card2, "btn4"); cardPanel.add(card3, "btn5"); cardPanel.add(card3, "btn6"); cardPanel.add(card1, "質問1"); cardPanel.add(card2, "質問2"); cardPanel.add(card3, "質問3"); /* カード移動用ボタン(前へ,次へ) */ JButton prevButton = new JButton("前へ"); prevButton.addActionListener(this); prevButton.setActionCommand("前へ"); JButton nextButton = new JButton("次へ"); nextButton.addActionListener(this); nextButton.setActionCommand("次へ"); JPanel btnPanel = new JPanel(); btnPanel.add(prevButton); btnPanel.add(nextButton); getContentPane().add(cardPanel, BorderLayout.CENTER); getContentPane().add(btnPanel, BorderLayout.PAGE_END); } public void actionPerformed(ActionEvent e){ String cmd = e.getActionCommand(); /* 点数 */ if (cmd.equals("btn1")){ n += 1; }else if (cmd.equals("btn2")){ n = n; } if (cmd.equals("btn4")){ n += 1; }else if (cmd.equals("btn3")){ n = n; } if (cmd.equals("btn5")){ n += 1; }else if (cmd.equals("btn6")){ n = n; } /* 解説 */ if (cmd.equals("btn1")||cmd.equals("btn2")){ String s=Integer.toString(n); JLabel label1 = new JLabel("解説1,正解数は"+s+"問です。"); JOptionPane.showMessageDialog(this, label1); } if (cmd.equals("btn3")||cmd.equals("btn4")){ String s=Integer.toString(n); JLabel label2 = new JLabel("解説2,正解数は"+s+"問です。"); JOptionPane.showMessageDialog(this, label2); } if (cmd.equals("btn5")||cmd.equals("btn6")){ String s=Integer.toString(n); JLabel label3 = new JLabel("解説3,正解数は"+s+"問です。これで問題は終了です。"); JOptionPane.showMessageDialog(this, label3); } /* カード移動用ボタン(前と次) */ if (cmd.equals("前へ")){ layout.previous(cardPanel); }else if (cmd.equals("次へ")){ layout.next(cardPanel); } } }
tatsusho👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

LouiS0616

2017/08/16 12:45

コードはバッククオート三つで括ってください。よくわからない場合は、マークダウン記法について調べてみてください。これをしているかどうかで、回答の量と質が変わってきます。
guest

回答1

0

ベストアンサー

とりあえず、次の点の改善をお勧めします。

  • リスナークラスを複数用意すること

『次へ』『前へ』を処理するクラスと、答えの選択を処理するクラスは分割すべきです。
一概に言えないことではありますが、actionPerformed内での条件分岐は避けたいところです。

  • 配列やリストを利用すること

それぞれの問題に対する選択肢は、[問題数][選択肢数]の配列で管理できます。
配列にすることで、似たような処理を簡潔に書くことができます。
また、答えも配列で管理するようにすれば、問題を追加したときも柔軟に対応できるでしょう。
(私の例では独自クラスの配列としています)


適当ですが、作ってみました。

Java

1public class Question { 2 private String question; 3 private String[] answerList; 4 private int answerNum; 5 private String comment; 6 7 public Question(String question, String[] answerList, int answerNum, String comment) { 8 this.question = question; 9 this.answerList = answerList; 10 this.answerNum = answerNum; 11 this.comment = comment; 12 } 13 14 public String getQuestion() { 15 return question; 16 } 17 public String[] getAnswerList() { 18 return answerList; 19 } 20 public String getComment() { 21 return comment; 22 } 23 24 public boolean isCorrect(String answer) { 25 return answer.equals(answerList[answerNum]); 26 } 27}

Java

1import javax.swing.*; 2import java.awt.CardLayout; 3import java.awt.BorderLayout; 4import java.awt.event.*; 5 6public class Sample extends JFrame{ 7 private int correctedNum = 0; 8 private int answeredNum = 0; 9 10 private final Question[] questions = { 11 new Question("質問1", new String[]{"選択肢1", "選択肢2"}, 0, "解説1"), 12 new Question("質問2", new String[]{"選択肢3", "選択肢4"}, 1, "解説2"), 13 new Question("質問3", new String[]{"選択肢5", "選択肢6"}, 0, "解説3"), 14 }; 15 16 public static void main(String[] args){ 17 new Sample("タイトル"); 18 } 19 20 private Sample(String title){ 21 super(title); 22 23 // 24 // CardPanel 25 JPanel cardPanel = new JPanel(); 26 CardLayout layout = new CardLayout(); 27 cardPanel.setLayout(layout); 28 29 for(Question question: questions) { 30 JPanel questionCard = new JPanel(); 31 questionCard.add(new JLabel(question.getQuestion())); 32 33 AnswerChecker answerChecker = new AnswerChecker(question); 34 for(String answer: question.getAnswerList()) { 35 JButton button = new JButton(answer); 36 questionCard.add(button); 37 button.addActionListener(answerChecker); 38 } 39 40 cardPanel.add(questionCard); 41 } 42 getContentPane().add(cardPanel, BorderLayout.CENTER); 43 QuestionChooser questionChooser = new QuestionChooser(layout, cardPanel); 44 45 // 46 // CtrlPanel 47 JPanel ctrlPanel = new JPanel(); 48 49 JButton prevButton = new JButton("前へ"); 50 prevButton.addActionListener(questionChooser); 51 prevButton.setActionCommand("previous"); 52 53 JButton nextButton = new JButton("次へ"); 54 nextButton.addActionListener(questionChooser); 55 nextButton.setActionCommand("next"); 56 57 ctrlPanel.add(prevButton); 58 ctrlPanel.add(nextButton); 59 60 // 61 // FrameLayout 62 add(ctrlPanel, BorderLayout.PAGE_END); 63 64 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 65 setBounds(10, 10, 300, 200); 66 setVisible(true); 67 } 68 69 private class QuestionChooser implements ActionListener { 70 private CardLayout layout; 71 private JPanel panel; 72 73 QuestionChooser(CardLayout layout, JPanel panel) { 74 this.layout = layout; 75 this.panel = panel; 76 } 77 78 public void actionPerformed(ActionEvent e) { 79 String cmd = e.getActionCommand(); 80 81 if( cmd.equals("next") ) { 82 layout.next(panel); 83 } 84 else if( cmd.equals("previous")) { 85 layout.previous(panel); 86 } 87 } 88 } 89 90 private class AnswerChecker implements ActionListener { 91 private Question question; 92 93 AnswerChecker(Question question) { 94 this.question = question; 95 } 96 97 public void actionPerformed(ActionEvent e) { 98 ++answeredNum; 99 100 String selectedAnswer = ((JButton)e.getSource()).getText(); 101 if(question.isCorrect(selectedAnswer)) { 102 ++correctedNum; 103 } 104 105 String comment = question.getComment() + "正答数は" + correctedNum + "/" + answeredNum + "です"; 106 showMessage(comment); 107 } 108 109 private void showMessage(String str) { 110 JOptionPane.showMessageDialog(null, new JLabel(str)); 111 } 112 } 113}

構造は突貫ですし、命名はいい加減です。参考程度にお願いします。

投稿2017/08/16 13:00

編集2017/08/17 00:04
LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Hanamarunanngo

2017/08/17 18:28

こんにちは。返信遅くなりすみません。すばらしい解答ありがとうございます。参考にさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問