【问题标题】:program that gives both the hcf and gcd of two numbers给出两个数字的 hcf 和 gcd 的程序
【发布时间】:2013-10-28 03:54:18
【问题描述】:

我是初学者,这是为学校准备的。请帮帮我,它必须是一个非常简单的程序,用户输入两个数字,结果是两个数字,一个 hcf 和一个 gcd。 我分别编写了代码,但我不知道如何组合它们。

【问题讨论】:

  • 我没有投反对票,但如果你能向我描述你想使用的算法,我会投赞成票。
  • 如果您在此处提供代码会很有帮助。
  • 基于观察:家庭作业 + Stackoverflow + 没有可见代码 + 对问题的了解最少 = 坏主意。
  • @JoshM 不管它是否是家庭作业,但如果 OP 显示了他尝试过的内容,那么它就可以了
  • @javaBeginner 非常正确。但很明显,OP 甚至都没有尝试过。

标签: java


【解决方案1】:

转到您的 GCD 程序,将计算 GCD 的代码移到一个函数中:

public int GCD(int a, int b) {
    //find GCD
    //return GCD
}

对 HCF 做同样的事情:

public int HCF(int a, int b) {
    //find HCF
    //return HCF
}

然后在main方法中:

//all the code for prompting the user for input
//and all the code for asking the user for input
//code which you've already written if you wrote these two programs independently already
System.out.println("GCD of " + input1 + " and " + input2 + " is " + gcd(input1, input2));
System.out.println("HCF of " + input1 + " and " + input2 + " is " + hcf(input1, input2));

【讨论】:

  • 我相信您正在寻找printf,而不是println
  • @JoshM 或者我刚刚修复的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2021-04-05
  • 1970-01-01
相关资源
最近更新 更多