Complete the following C# code that uses the DisplayPrice() method for displaying one price from the array of 4 prices. Because the DisplayPrice() method uses an array, an IndexOutOfRangeException might be thrown. Write a try block in which prompt the user for a subscript value and displays the appropriate price value stored in the corresponding array position. Create a catch block that catches any IndexOutOtRangeException, displays an appropriate error message and a price of $0.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
using System;
class MainClass {
public static void DisplayPrice(int index){
double[] prices = new double[5] {1200,1300,1400,1500,1600};
double priceAtIndex = prices[index];
Console.WriteLine("prices["+index+"] = $"+priceAtIndex.ToString("#.##"));
}
public static void Main (string[] args) {
int index;
Console.Write("Enter a index to display price: ");
index = Convert.ToInt32(Console.ReadLine());
try{
DisplayPrice(index);
}
catch(IndexOutOfRangeException e){
Console.WriteLine("Error: Invalid index entered.");
Console.WriteLine("Price: $0.00");
}
}
}
=================================================================
Get Answers For Free
Most questions answered within 1 hours.