-1

我正在尝试生成两个数字,以便除法结果是整数,而不是双精度数,例如:2/2=1; 30/3=10;等等...我不想要这样的东西:14/5=2.8; 18/7=2.57..等等。请帮忙。提前致谢。

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);
}
}
4

4 回答 4

6

您只需将两个数字相乘:a*b=c - 那么您有 c,所以当它除以 a 或 b 时,它是一个整数?

于 2013-02-02T06:58:10.067 回答
2

as suggested by @chris: here is what you can do:

Edit:

// `+1` as random number generated can be "0" which will result in `ArithmeticException`
int num1 = Random.nextInt(100)+1;  // enter range as required

int num2 = Random.nextInt(100)+1;

int divident = num1*num2;  // here is your first number

now choose any variable num1 or num2 as your second number as divident%num1 == 0 or divident%num2==0

Its lot better than keep on testing random numbers until they meet this criteria num1%num2==0

于 2013-02-02T07:11:43.070 回答
0

试试这个代码,希望它适用于你的逻辑

int randomInt2 = 0;
int randomInt1 = randomGenerator.nextInt(50)%20;
if(randomInt1 == 0){
   randomInt1 = randomGenerator.nextInt(50)%20;
}
ramdonInt2 = randomGenerator.nextInt(50)%20;
while((randomInt1 < randomInt2) && (randomInt1%randomInt2 != 0)){
  randomInt2 = randomGenerator.nextInt(50)%20;
}

现在你可以直接把剩下的逻辑放在任何地方if-else

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);
于 2013-02-02T06:58:50.033 回答
0
public void generateNumbers() {
 while(true) {
   int number1 = Random.nextInt(100);
   int number2 = Random.nextInt(100);
   if (number1 % number2 == 0) {
     System.out.println("Number " + number1 + " is divisible by " + number2);
     break;
   }
}

你可以写这样的东西。它生成两个 100 以内的随机整数。您可以更改整数范围以满足您的需要。规则是简单地选择整数,使得一个除以另一个不会产生余数。请注意,此代码未经测试。希望你能明白。

于 2013-02-02T07:00:02.323 回答