console.readline()出现问题;在c#中,无论我输入什么数字,都会将其转换为数字49
我对c#完全陌生,这是我的第一个程序console.readline()出现问题;在c#中,无论我输入什么数字,都会将其转换为数字49,c#,C#,我对c#完全陌生,这是我的第一个程序 namespace CPU_load { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to the Jackson CPU monitor"); Console.WriteLine("Press ESC at any time to exit"); /
namespace CPU_load {
class Program {
static void Main(string[] args) {
Console.WriteLine("Welcome to the Jackson CPU monitor");
Console.WriteLine("Press ESC at any time to exit");
//instructions_1 = Console.WriteLine("Would you like the view the instructions (y/n) ");
Console.Write("How often would you like to take a reading?(in seconds) ");
interval = Console.Read();
Console.WriteLine();
Console.WriteLine(interval);
interval_int1 = Convert.ToInt32(interval);
//int interval_int = (int)interval;
int interval_int_milliseconds = interval_int * 1000;
Console.WriteLine("A reading will be taken every {0} seconds", interval_int1);
Thread.Sleep(2500);
PerformanceCounter perfCPUcount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
PerformanceCounter perfMEMcount = new PerformanceCounter("Memory", "Available MBytes");
ConsoleColor oldColor = Console.ForegroundColor;
while (true) {
float cpuload = (int) perfCPUcount.NextValue();
float memload = (int) perfMEMcount.NextValue();
if (cpuload == 100) {
Console.ForegroundColor = ConsoleColor.Red;
}
if (cpuload <= 20) {
Console.ForegroundColor = ConsoleColor.Green;
}
Console.Write("CPU load {0}% ", cpuload);
Console.ForegroundColor = oldColor;
DateTime now = DateTime.Now;
Console.WriteLine(now);
Console.ForegroundColor = oldColor;
if (memload <= 500) {
Console.ForegroundColor = ConsoleColor.Red;
}
if (memload <= 1500) {
Console.ForegroundColor = ConsoleColor.Green;
}
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape) break;
Console.WriteLine("Available Memory {0}MB", memload);
Thread.Sleep(1000);
}
Console.ForegroundColor = oldColor;
}
public static int interval_int {
get;
set;
}
public static int interval {
get;
set;
}
public static int interval_int1 {
get;
set;
}
}
}
名称空间CPU\u加载{
班级计划{
静态void Main(字符串[]参数){
Console.WriteLine(“欢迎使用Jackson CPU监视器”);
Console.WriteLine(“随时按ESC退出”);
//instructions_1=Console.WriteLine(“您是否希望查看说明(y/n)”);
控制台。写下(“您希望多长时间读取一次数据?(秒)”;
interval=Console.Read();
Console.WriteLine();
控制台写入线(间隔);
interval_int1=转换为32(interval);
//int区间_int=(int)区间;
int interval_int_毫秒=interval_int*1000;
WriteLine(“每隔{0}秒读取一次”,间隔为1);
睡眠(2500);
PerformanceCounter perfCPUcount=新的PerformanceCounter(“处理器信息”,“处理器时间百分比”,“总计”);
PerformanceCounter performCount=新的PerformanceCounter(“内存”、“可用MB”);
ConsoleColor oldColor=Console.ForegroundColor;
while(true){
float cpuload=(int)perfCPUcount.NextValue();
float memload=(int)perfMEMcount.NextValue();
如果(cpuload==100){
Console.ForegroundColor=ConsoleColor.Red;
}
如果(cpuload您正在执行的是Console.Read();
,它只读取单个字符并返回其ASCII值
它输出49的原因是49是1
的ASCII值
您需要使用Console.ReadLine()
来获取输入的文本,然后使用类似int.TryParse
的方法将文本转换为数字表示形式。您正在执行的是Console.Read();
,它只读取单个字符并返回其ASCII值
它输出49的原因是49是1
的ASCII值
您需要使用Console.ReadLine()
要获得输入的文本,然后使用类似int.TryParse
的方法将文本转换为数字表示。我可以将其粘贴到哪里?您可以将代码放在问题中的同一位置…在您的问题中,或者只是将其粘贴到粘贴箱或其他东西上,我们可以为您编辑它…旁注:根据您提供的代码“无论我输入什么数字,我总是得到数字49”不可能是真的。我可以将它粘贴到哪里?将代码放在你的问题中的同一个位置…在你的问题中,或者只是将它粘贴到粘贴箱或其他东西上,我们可以为你编辑它…旁注:根据你提供的代码,“无论我输入什么数字,我总是得到数字49”不可能是真的。