我有这个代码:
public static void Main(string[] args)
{
if (string.IsNullOrEmpty(args[0])) // Warning : Index was out of the bounds of the array
{
ComputeNoParam cptern = new ComputeNoParam();
cptern.ComputeWithoutParameters();
}
else
{
ComputeParam cpter = new ComputeParam();
foreach (string s in args){...}
}
}
Run Code Online (Sandbox Code Playgroud)
也尝试过if(args.Length==0)
,但它仍然无法正常工作.
基本上我想知道用户是否使用参数调用程序.如果不是,程序将要求输入.
我怎样才能做到这一点?提前致谢.
所以我在这里有这个代码:
int n;
public static void Main(string[] args)
{
Console.Write("Please insert a number : ");
n = int.Parse(Console.ReadLine());
Console.Write("Please insert wait time (0,1 or 2) : ");
int time = int.Parse(Console.ReadLine())*1000;
Calculate(n,time);
}
Run Code Online (Sandbox Code Playgroud)
对于我来说,为多个n值(一个接一个地给出)调用Calculate(n,time)函数的最佳方法是什么,但是同一时间.我已经考虑使用数组来存储多个n值,但是有更好的选择.
另外,我想从命令行传递多个n作为参数.
有任何想法吗?提前致谢!
这是我第一次连接数据库,但是我遇到了一些问题
using Npgsql;
namespace DBPrj
{
class Program
{
static void Main(string[] args)
{
bool boolfound=false;
NpgsqlConnection conn = new NpgsqlConnection("Server=<ip>; Port=5432; User Id=Admin; Password=postgres.1; Database=Test1"); //<ip> is an actual ip address
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlDataReader dr= cmd.ExecuteReader(); //I get InvalidOperationException : The connection is not open.
if (dr.Read())
{
boolfound=true;
Console.WriteLine("connection established");
}
if(boolfound==false)
{
Console.WriteLine("Data does not exist");
}
dr.Close();
conn.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
}
可能是什么问题呢?NpgsqlConnection字符串是否正确写入?是否可以保护数据库免受远程访问?
我怎么能解决这个问题?
提前致谢!
我有以下代码的问题:
class Program
{
public static void Progress(ProgressEventArgs e)
{
int result = e.getPartialResult;
int stack_value = e.getValue ;
double max = System.Convert.ToDouble(numbers[j]);
System.Convert.ToDouble(stack_value);
double percent = (stack_value / max) * 100;
Console.CursorLeft = 18;
Console.Write(result + " ");
Console.CursorLeft = 46;
Console.Write(System.Convert.ToInt32(percent) + "% ");
}
public static void Calculate(int number, int time=0)
{
Factorial Fact = new Factorial();
Fact.Progression += new Factorial.ProgressEventHandler(Progress);
Console.Write("\n" + "Partial results : ");
Console.CursorLeft = 35;
Console.Write("Progress : ");
int Result = Fact.CalculateFactorial(number, …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
public object[] Dispatch(string arg)
{
int time;
int i = 0;
object[] array = new object[10];
if (int.Parse(arg) >= 0 && int.Parse(arg) <= 20)
{
array[i] = new ComputeParam(int.Parse(arg));
}
else
{
if (arg[0] == '/' && arg[1] == 't')
{
Options opt = new Options();
time = opt.Option(arg);
}
}
return array;
}
Run Code Online (Sandbox Code Playgroud)
我将参数传递给我的程序,ArgsParser
如果它们是数字则将它们放入数组中,或者如果参数类似,则设置延迟时间/t:=Max
.问题是我需要数组和时间,我不能返回两个值.我怎样才能解决这个问题?