定义一个字符串数组,提示用户输入"hello"将没一个字符当成一个元素输入字符数组,并将他们输出;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
int b;
string[] a = new string[5];
int i;
for (i = 0; i < 5; i++)
{
b = Console.Read();
//a[i] = char.Parse(b); 这里错了 应该怎么转换?
}
for (i = 0; i < 5; i++)
{
Console.Write("{0}", a[i]);
} }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
int b;
string[] a = new string[5];
int i;
for (i = 0; i < 5; i++)
{
b = Console.Read();
//a[i] = char.Parse(b); 这里错了 应该怎么转换?
}
for (i = 0; i < 5; i++)
{
Console.Write("{0}", a[i]);
} }
}
}
ch = (char) Console.Read(); string str;
str = Console.ReadLine();
string[] a = new string[5];
int i;
for (i = 0; i < 5; i++)
{
b = (char)Console.Read();
a = 这里该怎么转换?
}
for (i = 0; i < 5; i++)
{
Console.Write("{0}", a[i]);
}
class Test
{
static void Main(string[] args)
{
string b;
char[] c = new char[5];
int i;
b = args[0];
for (i = 0; i < 5; i++)
{
c[i] = b[i];
Console.Write("{0}", c[i]);
} }
}
F:\>Test hello
hello
class Test
{
static void Main(string[] args)
{
string b;
char[] c = new char[5];
int i;
b = args[0];
for (i = 0; i < 5; i++)
{
c[i] = b[i];
Console.WriteLine("{0}", c[i]);
} }
}
F:\>Test Hello
H
e
l
l
o
直接 Console.WriteLine("{0}", b[i]);就可以
{
class Program
{
static void Main(string[] args)
{
int b;
char[] a = new char[5];
int i;
foreath (i = 0; i < 5; i++)
{ b = Console.Read();
b.substring(i,1);
a[i] = char.Parse(b); }
for (i = 0; i < 5; i++)
{ Console.Write("{0}", a[i]);
} }
}
}
第15行报错 b = args[0];
改 for(i = 0; i < 5; i++)
而且你这个代码好像没有 输入啊?那怎么显示hello?莫非你的编译器不一样?
能帮忙讲下这个 b.substring(i,1);方法是怎么使用的么?
截取b字符串,从b的i个下标开始截取,每行截取j个字符
还是这里 b = Console.Read();
b.substring(i,1);
a[i] = char.Parse(b);