在循环中将用户输入插入到数组中,然后打印它,C#

huangapple go评论63阅读模式
英文:

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 textto 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&lt;String&gt; ingredient_names_list;
    		List&lt;String&gt; ingredient_units_list;
    		List&lt;int&gt; ingredient_quantity_list;
    		
    		String ingredient_name;
    		String unit_measure;
    		
    		int i;
    		int ingredient_num;
    		int quantity;
    		
    		
    		// Initialize lists ...
    		
    		ingredient_names_list = new List&lt;String&gt;();
    		ingredient_units_list = new List&lt;String&gt;();
    		ingredient_quantity_list = new List&lt;int&gt;();
    		
    		
    		// Loop to get ingredients ...
    		
    		ingredient_num = 0;
    		
    		Console.WriteLine( &quot;Enter name of ingredient &quot; + ( ingredient_num + 1 ) + &quot;:&quot; );
    		ingredient_name = Console.ReadLine();
    		
    		while ( !String.IsNullOrWhiteSpace( ingredient_name ) )
    		{
    		
    			ingredient_names_list.Add( ingredient_name );
    			
    			Console.WriteLine( &quot;Enter quantity of &quot; + ingredient_name + &quot;: &quot; );  
    		    quantity = Convert.ToInt32(Console.ReadLine());
    			ingredient_quantity_list.Add( quantity );
    			
    			Console.WriteLine( &quot;Enter unit of measurement for &quot; + ingredient_name + &quot;: &quot; );
    			unit_measure = Console.ReadLine();
    			ingredient_units_list.Add( unit_measure );
    			
    			ingredient_num++;
    			
    			Console.WriteLine( &quot;Enter name of &quot; + ( ingredient_num + 1 ) + &quot;:&quot; );
    			ingredient_name = Console.ReadLine();
    			
    		}
    		
    		// Display all ingredients ...
    		
    		for ( i = 0; i &lt; ingredient_names_list.Count; i++ )
    		{
    		
    			ingredient_name = ingredient_names_list[ i ];
    			quantity = ingredient_quantity_list[ i ];
    			unit_measure = ingredient_units_list[ i ];
    			
    			Console.WriteLine( &quot;Ingredient &quot; + ( i + 1 ) );
    			Console.WriteLine( &quot;- Name: &quot; + ingredient_name );
    			Console.WriteLine( &quot;- Quantity: &quot; + quantity );
    			Console.WriteLine( &quot;- Unit measure: &quot; + 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( &quot;Enter name of ingredient &quot; + ( ingredient_count + 1 ) + &quot;:&quot; );
		ingredient_name = Console.ReadLine();
		
		while ( !String.IsNullOrWhiteSpace( ingredient_name ) &amp;&amp; ingredient_count &lt; k_ingredients_count_max )
		{
		
			ingredient_names_array[ ingredient_count ] = ingredient_name ;
			
			Console.WriteLine( &quot;Enter quantity of &quot; + ingredient_name + &quot;: &quot; );  
		    quantity = Convert.ToInt32(Console.ReadLine());
			ingredient_quantity_array[ ingredient_count ] = quantity;
			
			Console.WriteLine( &quot;Enter unit of measurement for &quot; + ingredient_name + &quot;: &quot; );
			unit_measure = Console.ReadLine();
			ingredient_units_array[ ingredient_count ] = unit_measure;
			
			ingredient_count++;
			
			Console.WriteLine( &quot;Enter name of &quot; + ( ingredient_count + 1 ) + &quot;:&quot; );
			ingredient_name = Console.ReadLine();
			
		}
		
		// Display all ingredients ...
		
		for ( i = 0; i &lt; ingredient_count; i++ )
		{
		
			ingredient_name = ingredient_names_array[ i ];
			quantity = ingredient_quantity_array[ i ];
			unit_measure = ingredient_units_array[ i ];
			
			Console.WriteLine( &quot;Ingredient &quot; + ( i + 1 ) );
			Console.WriteLine( &quot;- Name: &quot; + ingredient_name );
			Console.WriteLine( &quot;- Quantity: &quot; + quantity );
			Console.WriteLine( &quot;- Unit measure: &quot; + unit_measure );
			
		}
		
		
	}
	
}

huangapple
  • 本文由 发表于 2023年3月31日 22:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定