"两个整数的最大公约数是最大的整数,它将两个数字中的每一个均分.写入方法Gcd返回两个整数的最大公约数.将方法合并到一个应用程序中,该用户从用户读取两个值并显示结果."
(这不是作业,只是我正在使用的书中的练习)
你能帮我解决这个问题吗?这是我到目前为止所得到的.谢谢
(编辑 - 我可以提交两个号码,但不会为我计算Gcd)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Greatest_Common_Divisor
{
class Program
{
static int GetNum(string text)
{
bool IsItANumber = false;
int x = 0;
Console.WriteLine(text);
do
{
IsItANumber = int.TryParse(Console.ReadLine(), out x);
} while (!IsItANumber);
return x;
}
static void Main(string[] args)
{
string text = "enter a number";
int x = GetNum(text);
text = "enter a second number";
int y = GetNum(text);
int z = GCD(x, y);
Console.WriteLine(z); …Run Code Online (Sandbox Code Playgroud) 我认为这很简单,但搜索谷歌似乎并没有帮助.
我基本上试图编写一个函数,当有两个整数(例如800和600)时,它会返回一个字符串比例(例如4:3).
string GetRatio(Int A, Int B) {
// Code I'm looking for
return Ratio;
}
Run Code Online (Sandbox Code Playgroud)