|
| 1 | +namespace HelloDotNetGuide.CSharp语法 |
| 2 | +{ |
| 3 | + public class GotoExercise |
| 4 | + { |
| 5 | + /// <summary> |
| 6 | + /// 使用goto重试进行代码重试示例 |
| 7 | + /// </summary> |
| 8 | + public static void GotoRetryUseExample() |
| 9 | + { |
| 10 | + int retryCount = 0; |
| 11 | + for (int i = 0; i < 10; i++) |
| 12 | + { |
| 13 | + retryLogic: |
| 14 | + try |
| 15 | + { |
| 16 | + //模拟可能出错的操作 |
| 17 | + Random random = new Random(); |
| 18 | + int result = random.Next(0, 2); |
| 19 | + |
| 20 | + if (result == 0) |
| 21 | + { |
| 22 | + throw new Exception("Error occurred"); |
| 23 | + } |
| 24 | + |
| 25 | + Console.WriteLine("Operation successful on attempt: " + retryCount); |
| 26 | + } |
| 27 | + catch (Exception ex) |
| 28 | + { |
| 29 | + retryCount++; |
| 30 | + if (retryCount < 3) |
| 31 | + { |
| 32 | + Console.WriteLine("Error occurred, retrying..."); |
| 33 | + goto retryLogic; //跳转到重试逻辑 |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + Console.WriteLine("Max retry limit reached."); |
| 38 | + return; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// 不使用goto重试进行代码重试示例 |
| 46 | + /// </summary> |
| 47 | + public static void NonGotoRetryUseExample() |
| 48 | + { |
| 49 | + int retryCount = 0; |
| 50 | + for (int i = 0; i < 10; i++) |
| 51 | + { |
| 52 | + while (retryCount < 3) |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + // 模拟可能出错的操作 |
| 57 | + Random random = new Random(); |
| 58 | + int result = random.Next(0, 2); |
| 59 | + |
| 60 | + if (result == 0) |
| 61 | + { |
| 62 | + throw new Exception("Error occurred"); |
| 63 | + } |
| 64 | + |
| 65 | + Console.WriteLine("Operation successful on attempt: " + retryCount); |
| 66 | + break; // 成功则跳出循环 |
| 67 | + } |
| 68 | + catch (Exception ex) |
| 69 | + { |
| 70 | + retryCount++; |
| 71 | + Console.WriteLine("Error occurred, retrying..."); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (retryCount == 3) |
| 76 | + { |
| 77 | + Console.WriteLine("Max retry limit reached."); |
| 78 | + return; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// goto正常输出使用示例 |
| 85 | + /// </summary> |
| 86 | + public static void GotoGeneralUseExample(int num) |
| 87 | + { |
| 88 | + if (num < 0) |
| 89 | + { |
| 90 | + goto LessThanZero; |
| 91 | + } |
| 92 | + else if (num == 0) |
| 93 | + { |
| 94 | + goto EqualToZero; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + goto GreaterThanZero; |
| 99 | + } |
| 100 | + |
| 101 | + LessThanZero: |
| 102 | + Console.WriteLine("数字小于零"); |
| 103 | + goto End; |
| 104 | + |
| 105 | + EqualToZero: |
| 106 | + Console.WriteLine("数字等于零"); |
| 107 | + goto End; |
| 108 | + |
| 109 | + GreaterThanZero: |
| 110 | + Console.WriteLine("数字大于零"); |
| 111 | + goto End; |
| 112 | + End: |
| 113 | + Console.WriteLine("End..."); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// 不使用goto正常输出使用示例 |
| 118 | + /// </summary> |
| 119 | + public static void NonGotoGeneralUseExample(int num) |
| 120 | + { |
| 121 | + if (num < 0) |
| 122 | + { |
| 123 | + Console.WriteLine("数字小于零"); |
| 124 | + } |
| 125 | + else if (num == 0) |
| 126 | + { |
| 127 | + Console.WriteLine("数字等于零"); |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + Console.WriteLine("数字大于零"); |
| 132 | + } |
| 133 | + Console.WriteLine("End..."); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments