英文:
Inserting user input to an array in a loop and then printing it, C#
问题
我应该如何将用户输入的值发送到一个循环中的数组,该数组将保存在内存中,然后如何打印数组中的所有值。(C#)
Console.WriteLine("输入成分名称:" + (i + 1) + ":");
Console.WriteLine("输入" + ingredient + "的数量:");
Console.WriteLine("输入" + ingredient + "的测量单位:");
这些是我需要在循环中保存的值,每次循环后都需要保存另一个值作为成分。例如,保存到数组中的第一个成分可能是苹果,第二个成分可能是香蕉,它们都需要保存到数组中,然后显示。
英文:
How do I send values from userinput to an array in a loop that will be saved in the array in memory and the how do I print all values in that array. (C#)
Console.WriteLine("Enter name of ingredient " + (i + 1) + ":");
Console.WriteLine("Enter quantity of " + ingredient + " :");
Console.WriteLine("Enter unit of measurment for " + ingredient + " :");
This is the values i would need to save in the loop and after every loop it would have to save another value as ingredient. For example the first ingredient saved to the array would be an apple and the second could be a banana and they both need your text
to be saved to the array and then displayed.
答案1
得分: -1
以下是您要翻译的内容:
以下可能会帮助您入门 - 使用列表而不是数组可能是一个更好的方法。您可以复制并粘贴下面的代码到[https://dotnetfiddle.net/][2],然后运行它。
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;
// 初始化列表...
ingredient_names_list = new List<String>();
ingredient_units_list = new List<String>();
ingredient_quantity_list = a new List<int>();
// 循环以获取配料...
ingredient_num = 0;
Console.WriteLine("输入配料名称 " + (ingredient_num + 1) + ":");
ingredient_name = Console.ReadLine();
while (!String.IsNullOrWhiteSpace(ingredient_name))
{
ingredient_names_list.Add(ingredient_name);
Console.WriteLine("输入" + ingredient_name + "的数量: ");
quantity = Convert.ToInt32(Console.ReadLine());
ingredient_quantity_list.Add(quantity);
Console.WriteLine("输入" + ingredient_name + "的计量单位: ");
unit_measure = Console.ReadLine();
ingredient_units_list.Add(unit_measure);
ingredient_num++;
Console.WriteLine("输入" + (ingredient_num + 1) + ":");
ingredient_name = Console.ReadLine();
}
// 显示所有配料...
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("配料 " + (i + 1));
Console.WriteLine("- 名称: " + ingredient_name);
Console.WriteLine("- 数量: " + quantity);
Console.println("- 计量单位: " + unit_measure);
}
}
}
<details>
<summary>英文:</summary>
The following may help you get started - [using Lists][1] instead of arrays might be a better approach. You can copy and paste the code below into: [https://dotnetfiddle.net/][2] and then run it.
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 );
}
}
}
[1]: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-7.0
[2]: https://dotnetfiddle.net/
</details>
# 答案2
**得分**: -1
以下是使用并行数组的另一个版本 - 希望能帮到你。
```csharp
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;
// 初始化数组...
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 ];
// 循环获取食材...
ingredient_count = 0;
Console.WriteLine("输入第" + (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("输入" + ingredient_name + "的数量:");
quantity = Convert.ToInt32(Console.ReadLine());
ingredient_quantity_array[ingredient_count] = quantity;
Console.WriteLine("输入" + ingredient_name + "的计量单位:");
unit_measure = Console.ReadLine();
ingredient_units_array[ingredient_count] = unit_measure;
ingredient_count++;
Console.WriteLine("输入第" + (ingredient_count + 1) + "个食材的名称:");
ingredient_name = Console.ReadLine();
}
// 显示所有食材...
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("食材 " + (i + 1));
Console.WriteLine("- 名称:" + ingredient_name);
Console.WriteLine("- 数量:" + quantity);
Console.WriteLine("- 计量单位:" + unit_measure);
}
}
}
英文:
Below is another version using parallel arrays - hope that helps.
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 );
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论