如何在特定条件下退出 while 循环以及对无效值的异常处理
How to exit while loop on certain condition and exception handling for invalid values
这是家庭作业,所以我只是想寻求一些指导。我的代码根据用户给定的索引值 return 数组 [10] 中的一个值。
不起作用的是我需要在输入“99”时停止程序,这个值也会触发 try-catch IndexOutOfRangeException。我无法让程序识别“99”和任何其他超出范围的值之间的区别。我确实用 if-else 尝试过,但是“99”仍然抛出 IndexOutOfRangeException 并且它不会循环。 do-while 的尝试也没有奏效。
代码如下。提前致谢。
using System;
using static System.Console;
class SubscriptExceptionTest
{
static void Main()
{
double[] array = {20.3, 44.6, 32.5, 46.7, 89.6, 67.5, 12.3, 14.6, 22.1, 13.6};
int i = 0;
while (i != 99)
try
{
Write("Enter a number to see the value in that position. Type 99 to stop: ");
i = Convert.ToInt32(ReadLine());
double arrayVal = array[i];
WriteLine("The value at index {0} is {1}", i, arrayVal);
ReadLine();
}
catch (FormatException fe)
{
throw new FormatException("You did not enter an integer.");
}
catch (IndexOutOfRangeException ie)
{
throw new IndexOutOfRangeException("Index was outside the bounds of the array.");
}
catch (Exception ex)
{
throw new Exception("Error: " + ex.Message);
}
}
}
问题是一旦你捕获了 IndexOutOfRangeException
你又把它扔进了 catch
块中,代码中的任何地方都没有处理。
另一个问题是,在使用它访问导致异常的数组中的 99 之前,您没有检查 i
的值。
见下文 -
int i = 0;
while (i != 99)
try
{
Write("Enter a number to see the value in that position. Type 99 to stop: ");
i = Convert.ToInt32(ReadLine());
if(i == 99) {
Console.WriteLine("Thanks!!, Breaking here!!!");
break;
}
double arrayVal = array[i];
WriteLine("The value at index {0} is {1}", i, arrayVal);
ReadLine();
}
catch (FormatException fe)
{
throw new FormatException("You did not enter an integer.");
}
catch (IndexOutOfRangeException ie)
{
// Show some message here
}
}
放置一个 if 结构来检查值 99 并在条件为真时退出应用程序。
i = Convert.ToInt32(ReadLine());
if( i == 99 )
{
//Perform any operation you want to before exiting the application.
Console.WriteLine("Exiting Application");
//This will terminate the program.
Environment.Exit(0);
}
作为规则,Try/Catch 和 异常处理程序应该只在异常情况下使用 。它们是昂贵的。让我向您展示如何使用防御性编程来处理这些问题。
我认为如果任何其他超出范围的值是您想要的答案,则使用Array.Length检查数组边界。
static void Main()
{
double[] array = {20.3, 44.6, 32.5, 46.7, 89.6,67.5, 12.3, 14.6, 22.1, 13.6};
int i = 0;
while (i != 99)
{
Write("Enter a number to see the value in that position. Type 99 to stop: ");
var result = ReadLine();
bool isInteger = int.TryParse(result, out i);
if (isInteger == false) throw new FormatException("You did not enter an integer.");
if (i == 99) Environment.Exit(0);
if (i < 0 || i > array.Length) throw new IndexOutOfRangeException("Index was outside the bounds of the array.");
double arrayVal = array[i];
WriteLine("The value at index {0} is {1}", i, arrayVal);
ReadLine();
}
这是家庭作业,所以我只是想寻求一些指导。我的代码根据用户给定的索引值 return 数组 [10] 中的一个值。
不起作用的是我需要在输入“99”时停止程序,这个值也会触发 try-catch IndexOutOfRangeException。我无法让程序识别“99”和任何其他超出范围的值之间的区别。我确实用 if-else 尝试过,但是“99”仍然抛出 IndexOutOfRangeException 并且它不会循环。 do-while 的尝试也没有奏效。
代码如下。提前致谢。
using System;
using static System.Console;
class SubscriptExceptionTest
{
static void Main()
{
double[] array = {20.3, 44.6, 32.5, 46.7, 89.6, 67.5, 12.3, 14.6, 22.1, 13.6};
int i = 0;
while (i != 99)
try
{
Write("Enter a number to see the value in that position. Type 99 to stop: ");
i = Convert.ToInt32(ReadLine());
double arrayVal = array[i];
WriteLine("The value at index {0} is {1}", i, arrayVal);
ReadLine();
}
catch (FormatException fe)
{
throw new FormatException("You did not enter an integer.");
}
catch (IndexOutOfRangeException ie)
{
throw new IndexOutOfRangeException("Index was outside the bounds of the array.");
}
catch (Exception ex)
{
throw new Exception("Error: " + ex.Message);
}
}
}
问题是一旦你捕获了 IndexOutOfRangeException
你又把它扔进了 catch
块中,代码中的任何地方都没有处理。
另一个问题是,在使用它访问导致异常的数组中的 99 之前,您没有检查 i
的值。
见下文 -
int i = 0;
while (i != 99)
try
{
Write("Enter a number to see the value in that position. Type 99 to stop: ");
i = Convert.ToInt32(ReadLine());
if(i == 99) {
Console.WriteLine("Thanks!!, Breaking here!!!");
break;
}
double arrayVal = array[i];
WriteLine("The value at index {0} is {1}", i, arrayVal);
ReadLine();
}
catch (FormatException fe)
{
throw new FormatException("You did not enter an integer.");
}
catch (IndexOutOfRangeException ie)
{
// Show some message here
}
}
放置一个 if 结构来检查值 99 并在条件为真时退出应用程序。
i = Convert.ToInt32(ReadLine());
if( i == 99 )
{
//Perform any operation you want to before exiting the application.
Console.WriteLine("Exiting Application");
//This will terminate the program.
Environment.Exit(0);
}
作为规则,Try/Catch 和 异常处理程序应该只在异常情况下使用 。它们是昂贵的。让我向您展示如何使用防御性编程来处理这些问题。
我认为如果任何其他超出范围的值是您想要的答案,则使用Array.Length检查数组边界。
static void Main()
{
double[] array = {20.3, 44.6, 32.5, 46.7, 89.6,67.5, 12.3, 14.6, 22.1, 13.6};
int i = 0;
while (i != 99)
{
Write("Enter a number to see the value in that position. Type 99 to stop: ");
var result = ReadLine();
bool isInteger = int.TryParse(result, out i);
if (isInteger == false) throw new FormatException("You did not enter an integer.");
if (i == 99) Environment.Exit(0);
if (i < 0 || i > array.Length) throw new IndexOutOfRangeException("Index was outside the bounds of the array.");
double arrayVal = array[i];
WriteLine("The value at index {0} is {1}", i, arrayVal);
ReadLine();
}