I am new to programming I am struggling with how to end this loop. The teacher is not helping to clarify.
The question I need to answer is "Ask the user to enter five days of the week and rainfall data for each day. Store the data in a two dimensional string array named rainfallData[]" Thanks so much!
String[,] rainFallData = new String[5, 2];
String name;
String rainFall;
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Enter the name of day " + i);
name = Console.ReadLine();
rainFallData[i - 1, 0] = name;
Console.WriteLine("Enter rainFall of day " + i);
rainFall = Console.ReadLine();
rainFallData[i - 1, 1] = rainFall;
for (int row = 0; row < 5; row++)
You can use break
. When you want to end the loop just write break;
.
Here is a solution that "closes the loop" by only asking for the info 5 times. It also shows how to write the data from the 2d array to the console after.
String[,] rainFallData = new String[5, 2];
for (int i = 0; i < rainFallData.GetLength(0); i++)
{
string name;
string rainFall;
Console.WriteLine("Enter the name of day " + i);
name = Console.ReadLine();
Console.WriteLine("Enter rainFall of day " + i);
rainFall = Console.ReadLine();
rainFallData[i, 0] = name;
rainFallData[i, 1] = rainFall;
}
for (int i = 0; i < rainFallData.GetLength(0); i++)
{
Console.WriteLine("Weekday: " + rainFallData[i,0]);
Console.WriteLine("Rainfall: " + rainFallData[i,1]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.