天天看點

最大公約數計算(Find the Greatest Common Divisior)

最大公約數計算(Find the Greatest Common Divisior)

// Find the Greatest Common Divisior of two integers

最大公約數計算(Find the Greatest Common Divisior)

// Java how to program, 5/e, Exercise 6.28

最大公約數計算(Find the Greatest Common Divisior)

import  javax.swing. * ;

最大公約數計算(Find the Greatest Common Divisior)

import  java.awt. * ;

最大公約數計算(Find the Greatest Common Divisior)

import  java.awt.event. * ;

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

public   class  GCalculation  extends  JApplet  implements  ActionListener  {

最大公約數計算(Find the Greatest Common Divisior)

 int n1,n2,G;

最大公約數計算(Find the Greatest Common Divisior)

 JLabel N1Label,N2Lable,GLable;

最大公約數計算(Find the Greatest Common Divisior)

 JTextField N1Field,N2Field,GField;

最大公約數計算(Find the Greatest Common Divisior)

 JTextArea output;

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 public void init()

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 {

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     Container  container=getContentPane();

最大公約數計算(Find the Greatest Common Divisior)

     container.setLayout(new FlowLayout());

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     N1Label= new JLabel("Enter the first integer:");

最大公約數計算(Find the Greatest Common Divisior)

     container.add(N1Label);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     N1Field=new JTextField(10);

最大公約數計算(Find the Greatest Common Divisior)

     container.add(N1Field);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     N2Lable= new JLabel("Enter the first integer:");

最大公約數計算(Find the Greatest Common Divisior)

     container.add(N2Lable);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     N2Field=new JTextField(10);

最大公約數計算(Find the Greatest Common Divisior)

     container.add(N2Field);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

    GLable= new JLabel("The Greated Common Divisor is:");

最大公約數計算(Find the Greatest Common Divisior)

     container.add(GLable);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     GField=new JTextField(10);

最大公約數計算(Find the Greatest Common Divisior)

     container.add(GField);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

    N2Field.addActionListener(this);

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 }

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 public void actionPerformed (ActionEvent event)

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 {

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

     n1=Integer.parseInt(N1Field.getText());

最大公約數計算(Find the Greatest Common Divisior)

     n2=Integer.parseInt(N2Field.getText());

最大公約數計算(Find the Greatest Common Divisior)

     for(int i=1;i<=Math.min(n1,n2);i++)

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

         {

最大公約數計算(Find the Greatest Common Divisior)

         if (n1%i==0&&n2%i==0)

最大公約數計算(Find the Greatest Common Divisior)

            G=i;

最大公約數計算(Find the Greatest Common Divisior)

         }

最大公約數計算(Find the Greatest Common Divisior)

     GField.setText(Integer.toString(G));

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 }

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

 }

最大公約數計算(Find the Greatest Common Divisior)
最大公約數計算(Find the Greatest Common Divisior)

繼續閱讀