【问题标题】:How can i enter from keyboard a phrase using an array?如何使用数组从键盘输入短语?
【发布时间】:2017-10-22 18:31:03
【问题描述】:

这是我理解必须做的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public const int N = 100;
        static void Main(string[] args)
        {
            char[] phrase = new char[N];
            int i=0;
            Console.WriteLine("enter a phrase: ");
            while ((i < N) && (phrase[i] != '.'))
            {
                phrase[i] = Convert.ToChar(Console.ReadLine());
                i++;
            }
             while(i<N){
                Console.WriteLine(+phrase[i]+"  ");
                i++;
            }
            Console.ReadLine();

        }
    }
}

我还想知道是否有一种方法可以输入短语,而无需为我介绍的每个字符按介绍

【问题讨论】:

  • ...Console.ReadLine 给你一整行作为string,这就是你想要的。
  • 那么如何使用 Console.ReadLine 来填充字符数组呢?
  • 如果你真的需要一个字符数组,使用ToCharArray方法。如果您只想表示一个有序的字符集合,请使用 string - 这是它的设计目的。

标签: c# arrays string char phrase


【解决方案1】:

除非我遗漏了什么,否则你可以这样做:

 static void Main(string[] args)
 {           
        Console.WriteLine("enter a phrase: ");
        string phrase = Console.ReadLine();           
        var chars = phrase.ToCharArray();  //If you want it as a char array
 }

Console.ReadLine() 将读取该行,直到用户点击返回/输入,我们可以将其存储为字符串短语。如果您确实想要 char 数组中的输入,您可以在字符串上使用 .ToCharArray() 函数

【讨论】:

  • 如果我想打印这个数组,我该怎么做呢?
  • @JordiGarcía 你会使用 Console.WriteLine(phrase);
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多