use*_*277 2 c# if-statement while-loop do-while
对不起,我是编程世界的新手.我正在创建一个控制台应用程序,基本上可以输入您是否要将摄氏温度转换为华氏温度,反之亦然.
我遇到的问题是:
"错误3无法在此范围内声明名为'choice'的局部变量,因为它会给'choice'赋予不同的含义,'choice'已在'parent或current'范围内用于表示其他内容"
我试着看其他例子,但它们远比我大脑现在所能理解的要复杂得多.
namespace TemperatureApp
{
class Program
{
static void Main(string[] args)
{
int choice;
do
{
Console.WriteLine("Hi! This is a temperatue app");
Console.WriteLine("Press 1 for C to F or 2 for F to C");
//take the user input
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1)
{
Console.WriteLine("Great, you chose C to F. Now enter the temp.");
int celcius = Convert.ToInt32(Console.ReadLine());
//next line use the formula and show answer
}
if (choice == 2)
{
Console.WriteLine("Great, you chose F to C. Now enter the temp.");
int fahrenheit = Convert.ToInt32(Console.ReadLine());
//next line use the formula and show answer
}
else
Console.WriteLine("That is not the right choice.");
//In this way, keep asking the person for either 1 or two
}
while (choice != 1 || choice != 2);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
int choice = Convert.ToInt32(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)
应该读
choice = Convert.ToInt32(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)
通过int
第二次放置,您在{}内声明另一个名为"choice"的变量.这个定义与外面的定义相冲突.