除算の結果が倍精度ではなく整数になるように、2つの数値を生成しようとしています。例:2/2 = 1; 30/3 = 10; など...私はこのようなものは必要ありません:14/5 = 2.8; 18/7=2.57..etc。助けてください。前もって感謝します。
package number;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class IntergerNumber extends JFrame implements ActionListener
{
JLabel lblFirstNum = new JLabel("");
JLabel lblSecondNum = new JLabel("");
JLabel lblOperator = new JLabel("/");
JTextArea taResult = new JTextArea();
JButton bCheck = new JButton("Check");
JPanel pOperator = new JPanel();
JPanel pButton = new JPanel();
public IntergerNumber()
{
pOperator.setLayout(new GridLayout(3, 2));
pOperator.add(new JLabel());
pOperator.add(lblFirstNum);
pOperator.add(lblOperator);
pOperator.add(lblSecondNum);
pOperator.add(new JLabel());
pOperator.add(taResult);
pButton.add(bCheck);
initializeComponents();
generateRanDomNumber();
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(pOperator, BorderLayout.CENTER);
this.getContentPane().add(pButton, BorderLayout.SOUTH);
}
private void generateRanDomNumber()
{
// TODO Auto-generated method stub
Random randomGenerator = new Random();
int randomInt2 = 0;
int randomInt1 = randomGenerator.nextInt(50)%20;
if(randomInt1 == 0)
{
randomInt1 = randomGenerator.nextInt(50)%20;
}
while((randomInt1%2) != 0) //this will avoid odd number
{
randomInt1 = randomGenerator.nextInt(50)%2;
if(randomInt1 == 0)
{
randomInt1 = randomInt1 + 2;
}
}
randomInt2 = randomGenerator.nextInt(50)%2;
if(randomInt2 == 0)
{
randomInt2 = randomGenerator.nextInt(50)%20;
}
while((randomInt2%2) != 0)
{
randomInt2 = randomGenerator.nextInt(50)%2;
if(randomInt2 == 0)
{
randomInt2 = randomGenerator.nextInt(50)%20;
}
}
if(randomInt1 >= randomInt2 && randomInt2 != 0)
{
System.out.println("====================");
System.out.println("First number is: " + randomInt1);
System.out.println("Second number is: " + randomInt2);
double x = randomInt1/randomInt2;
System.out.println("X is: " + x);
System.out.println("====================");
lblFirstNum.setText("" + randomInt1);
lblSecondNum.setText("" + randomInt2);
}
else if(randomInt1 < randomInt2 && randomInt1 != 0)
{
System.out.println("====================");
System.out.println("First number is: " + randomInt1);
System.out.println("Second number is: " + randomInt2);
double x = randomInt2/randomInt1;
System.out.println("X is: " + x);
System.out.println("====================");
lblFirstNum.setText("" + randomInt2);
lblSecondNum.setText("" + randomInt1);
}
}
private void initializeComponents()
{
// TODO Auto-generated method stub
lblOperator.setHorizontalAlignment(JLabel.RIGHT);
lblOperator.setFont(new Font("Arial", Font.BOLD, 40));
taResult.setBackground(Color.white);
taResult.setFont(new Font("Arial", Font.BOLD, 40));
taResult.setOpaque(true);
lblFirstNum.setHorizontalAlignment(JLabel.CENTER);
lblFirstNum.setFont(new Font("Arial", Font.BOLD, 40));
lblSecondNum.setHorizontalAlignment(JLabel.CENTER);
lblSecondNum.setFont(new Font("Arial", Font.BOLD, 40));
bCheck.addActionListener(this);
taResult.setFocusable(true);
taResult.requestFocus();
}
@Override
public void actionPerformed(ActionEvent ae)
{
// TODO Auto-generated method stub
if(ae.getSource().equals(bCheck))
{
String sFirstNum = lblFirstNum.getText();
String sSecondNum = lblSecondNum.getText();
String sResult = taResult.getText();
int first = Integer.parseInt(sFirstNum);
int second = Integer.parseInt(sSecondNum);
int result = Integer.parseInt(sResult);
if((first/second) == result)
{
System.out.println("Correct Answer");
taResult.setText("");
generateRanDomNumber();
taResult.setFocusable(true);
taResult.requestFocus();
}
else
{
System.out.println("Wrong Answer");
taResult.setText("");
generateRanDomNumber();
taResult.setFocusable(true);
taResult.requestFocus();
}
}
}
public static void main(String[] s)
{
IntergerNumber in = new IntergerNumber();
in.setVisible(true);
in.setSize(200,200);
in.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}