Try 块中的 C# 错误涉及循环、数组、下标和 IndexOutOfRangeException

发布于 2024-10-03 00:02:42 字数 1924 浏览 1 评论 0原文

我在下面包含了我的源代码草稿。如果您对我的错误做法有任何意见,我将不胜感激。我不确定我的语法是否正确...另外,我确实在 Cramster 上找到了一个示例;但我不确定该示例是否按照说明实现了“下标”(如果我错了,请指出)。我还认为“for”循环相当重复,因为它似乎正在建立与下标应该建立的相同的东西......此代码是为了响应以下作业:

“编写一个程序,在其中声明一个五个整数的数组并存储五个值 数组。编写一个 try 块,在其中放置一个循环来尝试访问每个 数组的元素,将下标从 0 递增到 10。创建一个 catch 块, 捕获最终的 IndexOutOfRangeException;在块内,显示“现在 你已经太过分了。”在屏幕上。将文件另存为 GoTooFar.cs。”

Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell

我的源代码有错误:

using System;

namespace Further

{
   public class GoTooFar
   {
      public static void Main()
      {
         private static int[] fiveIntArray = {1, 2, 3, 4, 5};
         //private static int CUTOFF = 11;    

         int subscript;
         int rate;


         try
         {
            //bool further;
            //public static int DetermineArray(int further)
            for(int x = 0; x < 10; x++)
            if(further < 11)
               throw new IndexOutofRangeException("Now you've gone too far.");
               subscript = 0;
            else
               subscript = 10;
            rate = fiveIntArray[subscript];
            return rate;
         }

         catch(IndexOutOfRangeException e)
         {
            throw;
            Console.WriteLine(e.StackTrace);
            //Console.WriteLine("Now you've gone too far.");
            //return e;
         }
      }
   }   
}                                       

//我在 Cramster.com 上找到的示例:

using System;
namespace Console2
{   
   class Class1
   {
      static void Main(string[] args)
      {
         int[] numbers = new int[5] {1, 2, 3, 4, 5};

         try
         {
            for(int i=0;i<10;i++)
            if(i>5)
               throw new IndexOutOfRangeException("Now you’ve gone too far.");
         }

         catch(IndexOutOfRangeException e)
         {
            throw;
         }

      }//end ma...
   }
}

I have included my source code draft below. I would appreciate any input on what I'm doing incorrectly. I'm not sure my syntax is correct... Also, I did find an example on Cramster; but I'm not sure that the example implemented the "subscript" as directed (please point it out if I am wrong in this) by the instructions. I also think that the "for" loop is rather repetitive since it appears that it's establishing the same thing that the subscript is supposed to establish... This code is in response to the following assignment:

"Write a program in which you declare an array of five integers and store five values in
the array. Write a try block in which you place a loop that attempts to access each
element of the array, incrementing a subscript from 0 to 10. Create a catch block that
catches the eventual IndexOutOfRangeException; within the block, display “Now
you’ve gone too far.” on the screen. Save the file as GoTooFar.cs."

Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell

My source code with errors:

using System;

namespace Further

{
   public class GoTooFar
   {
      public static void Main()
      {
         private static int[] fiveIntArray = {1, 2, 3, 4, 5};
         //private static int CUTOFF = 11;    

         int subscript;
         int rate;


         try
         {
            //bool further;
            //public static int DetermineArray(int further)
            for(int x = 0; x < 10; x++)
            if(further < 11)
               throw new IndexOutofRangeException("Now you've gone too far.");
               subscript = 0;
            else
               subscript = 10;
            rate = fiveIntArray[subscript];
            return rate;
         }

         catch(IndexOutOfRangeException e)
         {
            throw;
            Console.WriteLine(e.StackTrace);
            //Console.WriteLine("Now you've gone too far.");
            //return e;
         }
      }
   }   
}                                       

//The example I found on Cramster.com:

using System;
namespace Console2
{   
   class Class1
   {
      static void Main(string[] args)
      {
         int[] numbers = new int[5] {1, 2, 3, 4, 5};

         try
         {
            for(int i=0;i<10;i++)
            if(i>5)
               throw new IndexOutOfRangeException("Now you’ve gone too far.");
         }

         catch(IndexOutOfRangeException e)
         {
            throw;
         }

      }//end ma...
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-10-10 00:02:42

您不应该抛出异常,只需捕获运行时抛出的异常即可。还有其他几个问题:

  1. 您正在方法内声明一个字段。只需删除私有静态即可。
  2. for 循环中只有 if/else,但您显然也希望访问。我更喜欢始终使用花括号,但这是主观的。
  3. 您显然打算返回每次迭代,这是没有意义的。

它可以很简单:

public static void Main()
{
    int[] fiveIntArray = {1, 2, 3, 4, 5};
    try
    {
        for(int x = 0; x < 10; x++)
        {
            int rate = fiveIntArray[x];
        }
    }
    catch(IndexOutOfRangeException e)
    {
        Console.WriteLine("Now you've gone too far.");
    }       
}

注意,我们实际上并不使用rate。它就在那里,所以它将是一个有效的声明。

You're not supposed to throw an exception, just catch the one the runtime throws. There are several other issues:

  1. You are declaring a field inside a method. Just drop the private static.
  2. Only the if/else was in the for loop, but you clearly intend the access to be too. I prefer to always use curly braces, but that's subjective.
  3. You apparently intend to return every iteration, which doesn't make sense.

It can be as simple as:

public static void Main()
{
    int[] fiveIntArray = {1, 2, 3, 4, 5};
    try
    {
        for(int x = 0; x < 10; x++)
        {
            int rate = fiveIntArray[x];
        }
    }
    catch(IndexOutOfRangeException e)
    {
        Console.WriteLine("Now you've gone too far.");
    }       
}

Note, we don't actually use rate. It's just there so it will be a valid statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文