简体   繁体   English

使用 goto 时如何解决此编译错误“使用未分配的局部变量 'hak'”?

[英]How to resolve this compilation error “Using unassigned local variable 'hak'” when using goto?

I'm trying below code.我正在尝试下面的代码。 It gives me a warning "Use of unassigned local variable 'hak' ".它给了我一个警告“使用未分配的局部变量'hak'”。 I guess I'm missing something here.我想我在这里遗漏了一些东西。

I want it to display " 3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz. " when local variable "hak" equals to "0".当局部变量“hak”等于“0”时,我希望它显示“ 3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz. ”。 But it always display " Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. 2 hakkınız kalmıştır. "但它总是显示“ Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. 2 hakkınız kalmıştır.

                   Console.Write("Seçmek istediğiniz 'Kullanıcı Adı'nı belirtiniz: ");
        string kullanici_adi = Console.ReadLine();
        FARKLISIFRE:
        Console.Write("Lütfen Şifrenizi giriniz: ");
        string sifre = Console.ReadLine();
        Console.Write("Girmiş olduğunuz şifreyi tekrar giriniz: ");
        string sifre2 = Console.ReadLine();

        int karsilastirma = String.Compare(sifre, sifre2);
        if (karsilastirma==0)
        {
            Console.WriteLine("Tebrikler! Kaydınız başarılı bir şekilde oluşturulmuştur.");
        }
        else
        {
            Console.WriteLine("Girmiş olduğunuz şifreler birbirinden farklıdır. Lütfen tekrar deneyiniz.");
            goto FARKLISIFRE;
        }

        Console.Write("Giriş yapmak için lütfen '1' seçeneğini giriniz: ");
        char komut = Convert.ToChar(Console.ReadLine());
        Console.Clear();
        if (komut=='1')
        {
            goto GIRIS;
        }

        else
        {
            Console.Write("Çıkış yaptınız. İyi günler dileriz.");
            goto END;
        }
        
        int hak = 3;

        GIRIS:
        Console.Write("Lütfen Kullanıcı Adınızı Giriniz: ");
        string kullanici_adi_giris = Console.ReadLine();
        Console.Write("Lütfen belirlemiş olduğunuz Şifrenizi giriniz: ");
        string sifre_giris = Console.ReadLine();

        int karsilastirma_k_adi = String.Compare(kullanici_adi, kullanici_adi_giris);
        int karsilastirma_sifre = String.Compare(sifre, sifre_giris);
        
        if (karsilastirma_k_adi == 0 && karsilastirma_sifre == 0)
        {
            Console.Write("Başarıyla Giriş Yaptınız. Hoşgeldiniz.");
        }
        else if (hak == 0)
        {
            Console.Write("3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz.");
        }
        else
        {
            hak--;
            Console.WriteLine("Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. {0} hakkınız kalmıştır.", hak);
            goto GIRIS;
        }
        
        END:
        Console.ReadKey();
    }

You got this compile error because this line is never executed:您收到此编译错误,因为此行从未执行过:

int hak = 3;

It is because of the GOTOs, the position of the LABELS and the test condition if...else above this line: you never pass this line.这是因为 GOTO,标签的 position 和测试条件if...else在这条线之上:你永远不会通过这条线。

To solve this compile error as well as the design problem which is thus generated, you need to refactor all that to remove all the GOTOs.为了解决这个编译错误以及由此产生的设计问题,您需要重构所有这些以删除所有 GOTO。

Indeed, when compiling, before talking about executing, the if ( komut == '1' )... else... , the hak creation in never reached, going to GIRIS or END .实际上,在编译时,在谈论执行之前, if ( komut == '1' )... else...hak创作从未达到,去GIRISEND

Thus after when you try to use hak , it never was assigned and the compiler know that and tell you that.因此,当您尝试使用hak之后,它从未被分配过,编译器知道并告诉您。

Just to get to compile, you need to write:只是为了编译,你需要写:

GIRIS:
int hak = 3;

But using GOTOs is trashy and not clean, a very bad smell , as well as being source of mistakes and weird behaviors.但是使用 GOTO 是无用的、不干净的、非常难闻的气味,也是错误和怪异行为的来源。

在此处输入图像描述

Avoid goto more than one once per century, and use refactoring instead, please.请避免每个世纪不止一次 goto,而是使用重构

GOTO still considered harmful? GOTO 仍然被认为是有害的?

Spaghetti code意大利面条代码

Example of a minimal solution using a local method使用局部方法的最小解决方案示例

if ( komut == '1' )
{
  int hak = 3;
  GIRIS(ref hak);
}
else
{
  Console.Write("Çıkış yaptınız. İyi günler dileriz.");
}

Console.ReadKey();

void GIRIS(ref int hak)
{
  Console.Write("Lütfen Kullanıcı Adınızı Giriniz: ");
  string kullanici_adi_giris = Console.ReadLine();
  Console.Write("Lütfen belirlemiş olduğunuz Şifrenizi giriniz: ");
  string sifre_giris = Console.ReadLine();

  int karsilastirma_k_adi = String.Compare(kullanici_adi, kullanici_adi_giris);
  int karsilastirma_sifre = String.Compare(sifre, sifre_giris);

  if ( karsilastirma_k_adi == 0 && karsilastirma_sifre == 0 )
  {
    Console.Write("Başarıyla Giriş Yaptınız. Hoşgeldiniz.");
  }
  else if ( hak == 0 )
  {
    Console.Write("3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz.");
  }
  else
  {
    hak--;
    Console.WriteLine("Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. {0} hakkınız kalmıştır.", hak);
    GIRIS(ref hak);
  }
}

The compiler is seeing that the line of code that will define a value for the variable hak is before the label GIRIS.编译器看到将为变量hak定义值的代码行位于 label GIRIS之前 It cannot be guaranteed that the line will be executed, because GIRIS could be reached by the line of code goto GIRIS .不能保证该行会被执行,因为 GIRIS 可以通过goto GIRIS行到达。

Try changing this:尝试改变这个:

int hak = 3;
GIRIS:

to this:对此:

GIRIS:
int hak = 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM