所以,我试图制作一个给出三个数字的程序,我要看一个数字是否在其他两个数字之间,并说出第一个数字是否在其他数字之间。
我想在main()程序的外部使用bool,但是当我尝试调用在main中声明的变量时说:“名称'a'在当前上下文中不存在”
有什么方法可以在extern布尔值中使用main中声明的int吗? 我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
public static void Main()
{
Console.WriteLine("number to check");
int a; a = int.Parse(Console.ReadLine());
Console.WriteLine("1st range");
int rang1; rang1 = int.Parse(Console.ReadLine());
Console.WriteLine("2n range:");
int rang2; rang2 = int.Parse(Console.ReadLine());
if (EnRang() = true) { Console.WriteLine("Number {0} is between {1} and {2}", a, rang1, rang2); }
else if (EnRang() = false)
{ Console.WriteLine("The number {0} isn't between {1} and {2}", a, rang1, rang2); }
else { Console.WriteLine("Something goes wrong."); }
}
public static bool EnRang()
{
int NumerBool = a; int RangA = rang1; int RangB = rang2;
if (a > RangA || a < RangB){ return true; }
else{ return false; }
}
}
答案 0 :(得分:1)
这应该使您指向正确的方向。您需要将变量作为参数传递给EnRang
方法。
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("number to check");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("1st range");
int rang1 = int.Parse(Console.ReadLine());
Console.WriteLine("2n range:");
int rang2 = int.Parse(Console.ReadLine());
if (EnRang(a, rang1, rang2) == true)
{
Console.WriteLine("Number {0} is between {1} and {2}", a, rang1, rang2);
}
else if (EnRang(a, rang1, rang2) == false)
{
Console.WriteLine("The number {0} isn't between {1} and {2}", a, rang1, rang2);
}
else
{
Console.WriteLine("Something goes wrong.");
}
}
public static bool EnRang(int NumerBool, int RangA, int RangB)
{
if (NumerBool > RangA && NumerBool < RangB)
{
return true;
}
else
{
return false;
}
}
}