将用户的输入赋给一个变量

本文关键字:一个 变量 用户 输入 | 更新日期: 2023-09-27 18:16:47

我将代码缩减为这一小段,这样更容易阅读。我的问题是,用户写入"injtaille"的值并没有传递到稍微低一点的开关。我该如何解决这个问题?

if (inj == "oui")
{
    Console.WriteLine("Quel est la taille de l'injection? (30 - 50 - 60) ");
    injtaille = Convert.ToInt32(Console.ReadLine());
}
switch (client)
{
    case 1:
       consprix = 25;
       imgradprix = 55;
       analyzeprix = 28;
       switch (injtaille)
       {
           case 30
       }

将用户的输入赋给一个变量

问题是,当我把光标放在switch (injtaille)上时,它说我正在使用未分配的值

你需要在使用变量之前初始化。您的原始代码(从我可以看到)只有在保护inj == "oui"为真时才设置它。

试试这个:

int injtaille =0; // default value here
if (inj == "oui")
{
    Console.WriteLine("Quel est la taille de l'injection? (30 - 50 - 60) ");
    injtaille = Convert.ToInt32(Console.ReadLine());
}
switch (client)
{
    case 1:
       consprix = 25;
       imgradprix = 55;
       analyzeprix = 28;
       switch (injtaille)
       {
           case 30:
              ....
       }

根据程序的逻辑,在switch语句中也包含default通常是一个好主意。