搜索
熱搜: 活動 交友 discuz
查看: 1475|回復: 0
打印 上一主題 下一主題

[教學] IVE Lab 7-7 Sample

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-7-27 21:16:47 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
// IVE Lab 7-7

import javax.swing.*;

public class Lab_7_7_cal_of_gcd {
  
  static String num_1_st, num_2_st;
  static int gcd, num_1, num_2;
  static int result_1, result_2;
  
  public static void main(String[] args) {
   
    num_1_st = JOptionPane.showInputDialog(null, "Input the first number", "Find the Greater Common Diviser(GCD)", JOptionPane.QUESTION_MESSAGE);
   
    num_1 = Integer.parseInt(num_1_st);
   
    num_2_st = JOptionPane.showInputDialog(null, "Input the second number", "Find the Greater Common Diviser(GCD)", JOptionPane.QUESTION_MESSAGE);
   
    num_2 = Integer.parseInt(num_2_st);
   
    gcd = 1;
    for (int i = 1; i <= 5; i++) {
      
      switch (i) {
        
        case 1 :
          while (num_1 % 2 == 0 && num_2 % 2 == 0) {
          num_1 /= 2;
          num_2 /=2;
          gcd *= 2;
        }
          break;
      
        case 2 :
          while (num_1 % 3 == 0 && num_2 % 3 == 0) {
          num_1 /= 3;
          num_2 /= 3;
          gcd *= 3;
        }
          break;
      
        case 3 :
          while (num_1 % 5 == 0 && num_2 % 5 == 0) {
          num_1 /= 5;
          num_2 /= 5;
          gcd *= 5;
        }
          break;
        
        case 4 :
          if (num_2 != 1) {
          while (num_1 % num_2 == 0) {
            num_1 /= num_2;
            gcd *= num_2;
          }
        }
          break;
      
        case 5 :
          if (num_1 != 1) {
          while (num_2 % num_1 == 0) {
            num_2 /= num_1;
            gcd *= num_1;
          }
        }
          break;
      }
    }
    System.out.println("The GCD of those number is " + gcd);
  }
}
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-29 06:57 , Processed in 0.093270 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表