求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 阅读 7 次 更新于 2025-07-18 23:50:42 我来答关注问题0
  •  仰晴虹0Ci 求两道简单的java代码:1. 编写一个程序,要求在运行时,输入一个数字n,程序运行后,直接输出n!的结果

    第一题 import java.util.Scanner;public class First { public static void main(String[] args){ Scanner in=new Scanner(System.in);System.out.println("请输入一个数字");int n=in.nextInt();System.out.println(n+"的阶乘是:"+factorial(n));} public static int factorial(int m){ ...

  • 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...

  •  多彩生活小贴士 用java怎样编写登录页面,成功登录跳转到下一个页面,求代码

    1. 修正了方法签名为 `public void validateUserPass(String user, String pass)`,确保方法名和参数符合Java的命名惯例。```java public void validateUserPass(String user, String pass) { // ...} ```2. 引入了 `DBFactory` 类的实例化,并修正了数据库查询的方法,这里假设 `DBFactory` ...

  •  kejiaweiren 悬赏50.求一个JAVA游戏的代码,100行左右,要求每行都有相应的解释。用了什么类,什么方法。要自己写的。

    我帮你写一个猜拳游戏 import java.util.Random;import java.util.Scanner;public class Test { String[] array = new String[] { "布", "剪刀", "石头" };// 选项 Random r = new Random();//得到随机对象 private int player = 0;// 玩家获胜数 private int computer = 0;// 电脑...

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

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

Java相关话题

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