如何在循环中将userinput的值发送到数组中,该数组将保存在内存中的数组中,以及如何打印该数组中的所有值。(c#)
Console.WriteLine("Enter name of ingredient " + (i + 1) + ":");
Console.WriteLine("Enter quantity of " + ingredient + " :");
Console.WriteLine("Enter unit of measurment for " + ingredient + " :");
这是我需要在循环中保存的值,并且在每个循环之后,它都必须保存另一个值作为成分。例如,保存到数组中的第一种成分可能是苹果,第二种成分可能是香蕉,它们都需要将your text
保存到数组中然后显示。
以下内容可能会帮助您入门—使用list而不是数组可能是更好的方法。您可以将下面的代码复制并粘贴到:https://dotnetfiddle.net/,然后运行它。
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<String> ingredient_names_list;
List<String> ingredient_units_list;
List<int> ingredient_quantity_list;
String ingredient_name;
String unit_measure;
int i;
int ingredient_num;
int quantity;
// Initialize lists ...
ingredient_names_list = new List<String>();
ingredient_units_list = new List<String>();
ingredient_quantity_list = new List<int>();
// Loop to get ingredients ...
ingredient_num = 0;
Console.WriteLine( "Enter name of ingredient " + ( ingredient_num + 1 ) + ":" );
ingredient_name = Console.ReadLine();
while ( !String.IsNullOrWhiteSpace( ingredient_name ) )
{
ingredient_names_list.Add( ingredient_name );
Console.WriteLine( "Enter quantity of " + ingredient_name + ": " );
quantity = Convert.ToInt32(Console.ReadLine());
ingredient_quantity_list.Add( quantity );
Console.WriteLine( "Enter unit of measurement for " + ingredient_name + ": " );
unit_measure = Console.ReadLine();
ingredient_units_list.Add( unit_measure );
ingredient_num++;
Console.WriteLine( "Enter name of " + ( ingredient_num + 1 ) + ":" );
ingredient_name = Console.ReadLine();
}
// Display all ingredients ...
for ( i = 0; i < ingredient_names_list.Count; i++ )
{
ingredient_name = ingredient_names_list[ i ];
quantity = ingredient_quantity_list[ i ];
unit_measure = ingredient_units_list[ i ];
Console.WriteLine( "Ingredient " + ( i + 1 ) );
Console.WriteLine( "- Name: " + ingredient_name );
Console.WriteLine( "- Quantity: " + quantity );
Console.WriteLine( "- Unit measure: " + unit_measure );
}
}
}
下面是另一个使用并行数组的版本-希望对您有所帮助。
using System;
public class Program
{
public static void Main()
{
const int k_ingredients_count_max = 10;
String[] ingredient_names_array;
String[] ingredient_units_array;
int[] ingredient_quantity_array;
String ingredient_name;
String unit_measure;
int i;
int ingredient_count;
int quantity;
// Initialize arrays ...
ingredient_names_array = new String[ k_ingredients_count_max ];
ingredient_units_array = new String[ k_ingredients_count_max ];
ingredient_quantity_array = new int[ k_ingredients_count_max ];
// Loop to get ingredients ...
ingredient_count = 0;
Console.WriteLine( "Enter name of ingredient " + ( ingredient_count + 1 ) + ":" );
ingredient_name = Console.ReadLine();
while ( !String.IsNullOrWhiteSpace( ingredient_name ) && ingredient_count < k_ingredients_count_max )
{
ingredient_names_array[ ingredient_count ] = ingredient_name ;
Console.WriteLine( "Enter quantity of " + ingredient_name + ": " );
quantity = Convert.ToInt32(Console.ReadLine());
ingredient_quantity_array[ ingredient_count ] = quantity;
Console.WriteLine( "Enter unit of measurement for " + ingredient_name + ": " );
unit_measure = Console.ReadLine();
ingredient_units_array[ ingredient_count ] = unit_measure;
ingredient_count++;
Console.WriteLine( "Enter name of " + ( ingredient_count + 1 ) + ":" );
ingredient_name = Console.ReadLine();
}
// Display all ingredients ...
for ( i = 0; i < ingredient_count; i++ )
{
ingredient_name = ingredient_names_array[ i ];
quantity = ingredient_quantity_array[ i ];
unit_measure = ingredient_units_array[ i ];
Console.WriteLine( "Ingredient " + ( i + 1 ) );
Console.WriteLine( "- Name: " + ingredient_name );
Console.WriteLine( "- Quantity: " + quantity );
Console.WriteLine( "- Unit measure: " + unit_measure );
}
}
}