そしてこちらは、現在地点 9/24 ユークリッド互除法_Java
2ntブログ

スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

9/24 ユークリッド互除法_Java

ぼちぼちイラストなりコーディングなりやっていかんといけないのでね
仕事じゃ全然コーディングできねーし

ユークリッド互除法のJavaSwingプログラムです
2つの数の最大公約数を判定します

エラー判断とかはしてないのでお好きなように実装してください

int型の範囲内で入力しないとエラー吐いて止まります
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUIFoundation extends JFrame{
static final int WINDOW_WIDTH = 800;
static final int WINDOW_HEIGHT = 600;
JPanel panel;


GUIFoundation(){
this.setBounds(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
//画面サイズは共通のため。
panel= new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
this.add(panel);
this.setVisible(true);

}


}
//-------------ここまでをGUIFoundation.javaという名前で保存

//-------------ここから下全てをCalculateCommonDivisor.javaという名前で上で保存したファイルと同一階層に保存

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class CalculateCommonDivisor extends GUIFoundation implements ActionListener{
public JLabel explain1;
public JTextField numbox1;
public JLabel explain2;
public JTextField numbox2;
public JLabel answer;
public JButton button;
public int commonDivider;

static final int COMPONENT_WIDTH = 300;
static final int COMPONENT_HEIGHT = 20;
static final String CALCULATE_FLAG = "Calculate";

public static void main(String[] args){
CalculateCommonDivisor calculateCommonDivisor = new CalculateCommonDivisor();
calculateCommonDivisor.setTitle("最大公約数の計算ソフト");
}

CalculateCommonDivisor(){
super();
//SuperメソッドでGUIFoundationのコンストラクタ呼び出し
explain1 = new JLabel("1つ目の数字を入力");
explain2 = new JLabel("2つ目の数字を入力");
answer =new JLabel("最大公約数は ");
button = new JButton("計算する");

explain1.setBounds(0,0,COMPONENT_WIDTH ,COMPONENT_HEIGHT);
explain2.setBounds(300,0,COMPONENT_WIDTH ,COMPONENT_HEIGHT);
button.setBounds(400,200,COMPONENT_WIDTH,COMPONENT_HEIGHT);
button.setActionCommand(CALCULATE_FLAG);
button.addActionListener(this);

numbox1 = new JTextField("");
numbox2 = new JTextField("");

numbox1.setBounds(0,50,COMPONENT_WIDTH ,COMPONENT_HEIGHT);
numbox2.setBounds(300,50,COMPONENT_WIDTH,COMPONENT_HEIGHT);
answer.setBounds(0,200,COMPONENT_WIDTH,COMPONENT_HEIGHT);

//----------------------------大きさの設定ここまで


this.panel.add(explain1);
this.panel.add(explain2);
this.panel.add(numbox1);
this.panel.add(numbox2);

this.panel.add(answer);
this.panel.add(button);
repaint();





}
public void actionPerformed(ActionEvent e){
int num1 = Integer.parseInt(numbox1.getText());
int num2 = Integer.parseInt(numbox2.getText());
commonDivider = 0;
if(num1==num2){
commonDivider = num1;
return;
}
boolean underCalculation = true;

for(int i = 0;underCalculation;i++){

commonDivider = Math.max(num1,num2)%Math.min(num1, num2);
//大きい方から小さい方を割ったあまりを出す
if(num1>num2){
num1=commonDivider;
}else{
num2 = commonDivider;
}
//大きい方を余りに置き換える
if(commonDivider==0){
commonDivider = Math.max(num1,num2);
underCalculation = false;
//これで計算終了
}if(commonDivider ==1){
commonDivider =1;
underCalculation = false;
}
}
System.out.println("CommonDividerは" + commonDivider);
System.out.println("num1は" + num1);
answer.setText(Integer.parseInt(numbox1.getText()) + "と"+Integer.parseInt(numbox2.getText())+"最大公約数は" + commonDivider + "です");
repaint();

}

}

コメントの投稿

非公開コメント

検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QR