如何生成两个数字,以便除法的结果是整数?

时间:2013-02-02 06:51:43

标签: java

我正在尝试生成两个数字,以便除法结果是整数,而不是双数,例如:2/2 = 1; 30/3 = 10;等...我不想要这样的东西:14/5 = 2.8; 7分之18= 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);
}
}

4 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

根据@chris的建议: 这是你能做的:

修改

// `+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

现在选择任意变量num1num2作为您的第二个号码divident%num1 == 0divident%num2==0

它比继续测试随机数要好,直到它们满足这个标准num1%num2==0

答案 2 :(得分: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);

答案 3 :(得分: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内生成两个随机整数。您可以更改整数范围以满足您的需要。规则是简单地选择整数,使得一个对另一个的除法不会产生余数。请注意,此代码未经过测试。希望你明白这一点。