gpt4 book ai didi

c# for 循环在 switch/case 情况下 - 如何?

转载 作者:行者123 更新时间:2023-12-02 08:06:59 28 4
gpt4 key购买 nike

我正在尝试用 C# 扩展给定的代码,但由于缺乏编程经验,我有点陷入困境。

使用 Visual Studio 社区,我尝试通过控制台读出 CPU 核心温度。该代码使用开关/外壳来查找传感器的特定名称(即CPU Core #1)的外壳,并给出温度。对于每个 CPU 核心。有 64 个核心,这完全是复制粘贴。我尝试实现 foreach-situation 但它不起作用。另外我不知道如何在这里设置 for 循环。

这就是有问题的代码:

if (s.SensorType == LibreHardwareMonitor.Hardware.SensorType.Temperature)
{
if (s.Value != null)
{
float curTemp = (float)s.Value;
switch (s.Name)
{
case "Core Average": //average temps
cpuTemp = curTemp.ToString();
Console.WriteLine(" ... ");
Console.WriteLine(" CPU AVG TEMP: " + cpuTemp);
Console.WriteLine(" ... ");
break;

case "CPU Core #": //Temps for CPU Core #1, CPU Core #2 ...
foreach (s.Name in LibreHardwareMonitor...? ) // <-- no clue what to do here
{
int i = 1;
cpuTemp = curTemp.ToString();
Console.WriteLine(" ... ");
Console.WriteLine(" CPU #" + i +" TEMP: " + cpuTemp);
Console.WriteLine(" ... ");
i++;
}
break;
}
}
}

我陷入困境,不知道如何实现吐出所有 CPU Core#1CPU Core#64 温度。

完整代码如下:

private void dataCheck(object StateObj)
{
string cpuTempA = "";
string cpuTemp = "";
string gpuTemp = "";
string gpuLoad = "";
string cpuLoad = "";
string dataUsed = "";
string fanUsed = "";
string cpuClock = "";
//;
StateObjClass State = (StateObjClass)StateObj;
// enumerating all the hardware
foreach (LibreHardwareMonitor.Hardware.IHardware hw in thisComputer.Hardware)
{
Console.WriteLine("HARDWARE: " + hw.HardwareType);
Console.ReadLine();

hw.Update();
// searching for all sensors and adding data to listbox
foreach (LibreHardwareMonitor.Hardware.ISensor s in hw.Sensors)
{
Console.WriteLine("NAME: " + s.Name + ", TYPE: " + s.SensorType + ", VALUE: " + s.Value);
Console.ReadLine();

if (s.SensorType == LibreHardwareMonitor.Hardware.SensorType.Temperature)
{
Console.WriteLine(" FOUND " + s.SensorType);
if (s.Value != null)
{
Console.WriteLine(" FOUND VALUE " + s.Value);
float curTemp = (float)s.Value;
switch (s.Name)
{
case "Core Average":
Console.WriteLine(" FOUND NAME " + s.Name);
cpuTempA = curTemp.ToString();

Console.WriteLine(" ... ");
Console.WriteLine(" CPU AVG TEMP: " + cpuTempA);
Console.WriteLine(" ... ");
break;

case "CPU Core #": //CPU Core #1, CPU Core #2 ...
foreach (s.Name in LibreHardwareMonitor...? )
{
int i = 1;
cpuTemp = curTemp.ToString();
Console.WriteLine(" ... ");
Console.WriteLine(" CPU #" + i +" TEMP: " + cpuTemp);
Console.WriteLine(" ... ");
i++;
}
break;

}
}
}

if (s.SensorType == LibreHardwareMonitor.Hardware.SensorType.Clock)
{
Console.WriteLine(" FOUND " + s.SensorType);
if (s.Value != null)
{
Console.WriteLine(" FOUND VALUE " + s.Value);
float curClock = (float)s.Value;
switch (s.Name)
{
case "CPU Core #4":
Console.WriteLine(" FOUND NAME " + s.Name);
cpuClock = curClock.ToString();
Console.WriteLine(" ... ");
Console.WriteLine(" CPU CLOCK: " + cpuClock);
Console.WriteLine(" ... ");
break;
}
}
}
}
}
}
}

最佳答案

分割字符串并将#后面的部分转换为整数

if (s.Name.Contains("#")) {
string core_id_str = s.Name.Split('#')[1]; // gets second part of split
int core_id = System.Convert.ToInt32(core_id_str); // convert to integer
// do your magic
}

关于c# for 循环在 switch/case 情况下 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284806/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com