2013-02-02 54 views
-1

我想生成兩個數字,以便除法結果是一個整數,而不是雙倍,例如: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); 
} 
} 
+2

您目前的生成情況如何?有什麼限制? – smk

+3

'a%b == 0'是檢查 –

回答

6

你只是兩個數相乘在一起:A * B = C - 那麼你有C,這樣當它是由a或b劃分,它是一個整數?

+0

+1好方法 – exexzian

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

而現在,現在你可以直接把你剩餘的邏輯wihtout任何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); 
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內產生兩個隨機整數。你可以改變整數的範圍以滿足你的需要。規則是簡單地選擇整數,使得一個整數除以另一個整數不會導致餘數。請注意,此代碼未經測試。希望你明白這個主意。

2

由@克里斯建議: 這裏是你可以做什麼:

編輯:

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

+1的一種方法,您可能需要在隨機數上+1。 ;) –

+0

@PeterLawrey噢!很好,現在編輯 – exexzian

相關問題