"两个整数的最大公约数是最大的整数,它将两个数字中的每一个均分.写入方法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) 我想把华氏温度转换成摄氏温度.
做以下我总是得到零:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Celcius_Farenheit_Converter
{
class Program
{
static double Celcius(double f)
{
double c = 5/9*(f - 32);
return c;
}
static void Main(string[] args)
{
string text = "enter a farenheit tempature";
double c = Celcius(GetTempature(text));
Console.WriteLine("the tempature in Celicus is {0}", c);
Console.ReadKey(true);
}
static double GetTempature(string text)
{
Console.WriteLine(text);
bool IsItTemp = false;
double x = 0;
do
{
IsItTemp = double.TryParse(Console.ReadLine(), out x);
} while …Run Code Online (Sandbox Code Playgroud) 你可以帮我做下面的运动吗?(这不是家庭作业,只是我正在使用的书中的练习.)
"一个整数被认为是一个完美的数字,如果它的因素,包括一个(但不是数字本身),总和到数字.例如,6是一个完美的数字,因为6 = 1 + 2 + 3.写方法完美确定参数值是否为完美数字.在确定并显示2到1000之间的所有完美数字的应用程序中使用此方法.显示每个完美数字的因子以确认数字确实完美."
所以这就是我到目前为止所得到的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perfect_Numbers2
{
class Program
{
static bool IsItPerfect(int value)
{
int x = 0;
int counter = 0;
bool IsPerfect = false;
List<int> myList = new List<int>();
for (int i = value; i <= value; i++)
{
for (int j = 1; j < value; j++)
{
// if the remainder of i divided by j is zero, then j …Run Code Online (Sandbox Code Playgroud) (这不是作业,只是我正在使用的书中的练习)
"一个整数被认为是一个完美的数字,如果它的因素,包括一个(但不是数字本身),总和到数字.例如,6是一个完美的数字,因为6 = 1 + 2 + 3.写方法完美确定参数值是否为完美数字.在确定并显示2到1000之间的所有完美数字的应用程序中使用此方法.显示每个完美数字的因子以确认数字确实完美."
问题是它显示两次完美的数字而不是一次.它为什么这样做?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perfect_Numbers2
{
class Program
{
static bool IsItPerfect(int value)
{
int x = 0;
bool IsPerfect = false;
List<int> myList = new List<int>();
for (int i = value; i == value; i++)
{
for (int j = 1; j < i; j++)
{
if (i % j == 0) // if the remainder of i divided by j is zero, …Run Code Online (Sandbox Code Playgroud) 我在firstName和lastName收到错误"冲突变量定义如下".
另外'从不使用局部变量firstName,在此范围内不能声明名为firstName的局部变量....等'
编辑=这不是家庭作业,只是我正在使用的书中的练习.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsPractice
{
class Program
{
static string SwitchName(string x, string y)
{
string firstName = x;
string lastName = y;
string temp = firstName;
firstName = lastName;
lastName = temp;
string final = ("{0},{1}", firstName, lastName)
return final;
}
static void Main(string[] args)
{
string nameReversed = "";
string first = "Tim";
string last = "Stern";
nameReversed = SwitchName(first, last);
Console.WriteLine(nameReversed);
Console.ReadKey(true);
}
}
} …Run Code Online (Sandbox Code Playgroud)