using System;
//Find the square root of a number for 10 values from user
class forLoop
{
static void Main
{
double x;
for(int i=10; i>0 && x>=0; i--)
{
Console.WriteLine("You have {0} remaining calculations left", i);
Console.Write("Please enter a positive number: ");
x = double.Parse((Console.ReadLine());
x = Math.Sqrt(x);
Console.WriteLine("The square root is {0}", x);
Console.WriteLine("");
}
Console.WriteLine("You have 0 remaining calculations left");
}
}
Run Code Online (Sandbox Code Playgroud)
我需要有关此C#问题的帮助:为什么错误:"get get或set accessor expected"会在编译时出现?
你错过了()方法声明.因此,编译器在某种程度上认为您正在声明一个属性(尽管它会抛出关于该void类型的错误),而不是方法
// Property
public int Property
{
get { return _field; }
set { _field = value; }
}
// Property, albeit a get-only property
public int Property => _field;
// Method
public int Method()
{
return _field;
}
// Method
public int Method() => _field;
Run Code Online (Sandbox Code Playgroud)
更新:由于仍然可以看到,我已更新示例值以更好地反映其基础类型,并包含C#6引入的表达式主体的示例