If 语句嵌套在 C# 中不起作用 [关闭]

问题描述 投票:0回答:0

提前抱歉,我是编程新手。我尝试在嵌套的 for 中使用 if 语句,当我运行程序时,if 语句无法按我想要的方式工作!我的以下程序有什么问题?请指导我

double[] a = { 1, 1.1, 1.3, 1.8, 2, 2.2, 3, 3.2, 4, 5.5, 8.2 };
double c = 1000;
      
bool state = false;
      
for (int i = 0; i < 10; i++)
{
   double pow = Math.Pow(10, i);
        
   for (int x = 0; x < a.Length; x++)
   {
      double c1 = a[x] * pow;
            
      for (int p = 0; p < a.Length; p++)
      {
         double c2 = a[p] * pow;
                
         Console.WriteLine(c1.ToString() + " + " + c2.ToString() + " = " + (c1 + c2).ToString() + "(" + c.ToString() + ")");
         
         if (c1 + c2 == c)
         {
            state = true;
            goto END;
         }
         else state = false;
      }
   }
}
      
END:
if (state) Console.WriteLine("Get It");
else Console.WriteLine("Nothing");

我试过用 while 替换 for 但还是不行

c# for-loop if-statement nested-loops nested-for-loop
© www.soinside.com 2019 - 2024. All rights reserved.