求JAVA代码

int n){ int i = 1; int commyue = 0; int c = m; if (c < n) c = n; while (i
求JAVA代码
你好:
楼上已经贴出了代码,我在此只回答第3小问
static关键字表示的是静态方法,此处在main方法中可以直接调用gcd方法.
按照楼上的代码,如果去掉static,需要先初始化一下ComputeGCD类 再去用类的实例去调用方法。如下:
public class ComputeGCD {public int gcd(int m,int n){ int r = m % n; while (r!=0) { m = n; n = r; r = m % n; } return n; } public static void main(String[] args){ ComputeGCD c = new ComputeGCD(); System.out.println("gcd(24,16)="+c.gcd(24,16)); System.out.println("gcd(255,25)="+c.gcd(255,25)); }}2013-11-15
程序如下:
public class ComputeGCD { public static void main(String[] args) { int t = gcd(24,16); System.out.println("gcd(24,16) = " +t); int t1 = gcd(225,25); System.out.println("gcd(225,25)"+t1); } public static int gcd(int m,int n){ int i = 1; int commyue = 0; int c = m; if (c < n) c = n; while (i <= c) { if (m % i == 0 && n % i == 0) commyue = i; i++; } return commyue; }}

2 .运行结果
gcd(24,16) = 8gcd(225,25) = 25

3.去掉保留字static报:Cannot make a static reference to the non-static method gcd(int, int) from the type ComputeGCD
2013-11-15
public class ComputeGCD{ public static int gcd(int m,int n) { int r=m%n; while (r!=0) { m=n; n=r; r=m%n; } return n; } public static void main(String[] args) { System.out.println("gcd(24,16)="+gcd(24,16)); System.out.println("gcd(255,25)="+gcd(255,25)); /*输出结果 gcd(24,16)=8 gcd(255,25)=5 */ }}2013-11-15
mengvlog 阅读 259 次 更新于 2025-09-10 14:17:50 我来答关注问题0
  •  海南加宸 高分求 JAVA语言代码piglatin转换

    以下是完整的代码:import java.util.Scanner;public class Testing { public static void main(String[] args) { System.out.println("===");System.out.println("CPSC 1150 Lab 01 on 09/01/06 by Harry Wu 100131676");System.out.println("Translate the input to either English or Pig L...

  • anonymous 求java 输出正方形(用*号)的代码!

    public static void main(String[] args) { Scanner s = new Scanner(System.in);System.out.println("请输入正方形的大小:");int line = s.nextInt();; // 正方形的边长 for (int i = 1; i

  • public class TestStar { public static void main(String[] args) { String star = "*";for (int i = 0; i < 5; i++) { if (i == 0) { System.out.print(" " + star);System.out.println();} if (i == 1) { for (int z = 0; z < 4; z++) { System.out.prin...

  • 首先,我们定义一个名为Hello的类。在类中,我们定义了一个名为main的方法,这是Java程序的入口点。main方法接收一个String数组作为参数。在main方法内部,我们调用了System.out.println方法来输出一条消息。这条消息将被打印到控制台,内容是"Hello!!!"。编写好上述代码后,将其保存为Hello.java文件。...

  •  jimmyc2006 求java代码

    import java.util.Date;class Person{ //身份证号 private String number;//姓名 private String name;//出生日期 private Date birthday;public Date getBirthday() { return birthday;} public void setBirthday(Date birthday) { this.birthday = birthday;} public String getName() { return name...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部