[JAVA] Simple brain teaser calculation

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Kisan extends JFrame {

    public static void main(String[] args) {
        Game a = new Game();
        a.setVisible(true);
    }
}

class Game extends JFrame implements ActionListener {

    public JLabel mon;//Character in question
    public JLabel sei2;//Correct letter
    public String saishosei = "Solve this";//First letter
    public int awase;//Combined number
    public JTextField ko;//Answer field answer
    public int sho, tugi;//Two additions
    public JLabel sei3;//Correct label

    Game() {
        /*basic configuration*/
        super("calculator");
        this.setBounds(600, 500, 450, 300);//Overall position
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Processing at the end
        this.setVisible(true);//Visualization
        /*Text making*/
        /*Button below*/
        JPanel p = new JPanel();
        ko = new JTextField(30);//Text size
        JButton tou = new JButton("Don");//Button letter
        tou.addActionListener(this);//Receive button event
        ko.addActionListener(this);//Receive text event
        p.add(ko);
        p.add(tou);
        Container contentPane = getContentPane();//Get the whole
        contentPane.add(p, BorderLayout.SOUTH);//Adjusting the position of the button
        /*Making the above problem*/
        Random rnd = new Random();//Lower 4 lines Random numbers
        Random rnd2 = new Random();
        sho = rnd.nextInt(100);
        tugi = rnd2.nextInt(100);
        awase = sho + tugi;
        /*Get the correct answer in the center*/
        JPanel sei = new JPanel();
        sei.setLayout(null);//First, make the panel layer null
        sei3 = new JLabel(saishosei);//Correct answer
        mon = new JLabel(sho + "+" + tugi);//Fill in the letters in question
        sei3.setFont(new Font("MS gothic", Font.BOLD, 30));//Specify the size of the correct answer
        mon.setFont(new Font("Arial", Font.PLAIN, 30));//Specify the size of the problem
        sei3.setBounds(100, 130, 500, 35);//Specify the location of the correct answer
        mon.setBounds(170, 90, 600, 35);//Specifying the location of the problem
        sei.add(sei3);
        sei.add(mon);
        contentPane.add(sei, BorderLayout.CENTER);//Position of correct answer
    }

    /*Function for receiving events*/
    public void actionPerformed(ActionEvent ev) {
        String ko2 = "";//Initialization
        boolean frg = true;//flag
        //System.out.println(ko2);//For confirmation
        ko2 = ko.getText();//Get characters from text
        if (ko.getText().equals("") || ko2 == "") {
            sei3.setForeground(Color.BLACK);//Color specification
            sei3.setBounds(56, 130, 500, 35);//Specify the location of the correct answer
            sei3.setText("Please enter characters");
            frg = false;
        }
        if (frg == true) {//if1
            awase = sho + tugi;//Coalescence
            try {
                int ko4 = Integer.parseInt(ko2);//Type conversion
                if (ko4 == awase) {//if2
                    sei3.setText("Is the correct answer");
                    sei3.setForeground(Color.RED);//Color specification
                    sei3.setBounds(148, 130, 500, 35);//Specify the location of the correct answer
                    ko.setText("");//Erase characters
                    Random rnd = new Random();//Lower 4 lines Random numbers
                    Random rnd2 = new Random();
                    sho = rnd.nextInt(100);
                    tugi = rnd2.nextInt(100);
                    mon.setText(sho + "+" + tugi);
                } else {
                    sei3.setText("It's off");
                    sei3.setForeground(Color.BLUE);//Color specification
                    sei3.setBounds(130, 130, 500, 35);//Specify the location of the correct answer
                    ko.setText("");//Erase characters
                }//if2 end
            }//end of try
            catch (NumberFormatException e) {
                sei3.setText("Please enter the letters of the numbers");//When non-numeric characters are written
                sei3.setForeground(Color.BLACK);//Color specification
                sei3.setBounds(10, 130, 500, 35);//Location designation
                ko.setText("");//Erase characters
            }//end of catch
        }/*Event receiving function end*/
    }//if1 end
}//class end

A game that just adds up with eternity.

Recommended Posts

Simple brain teaser calculation
[Brain teaser] Reverse Polish notation enumeration and its calculation