首先,我想說對不起,因爲對於你們來說非常容易的問題,因爲我是初學者,我正在嘗試編寫一個程序來接受字符串中的輸入並將其反轉爲新字符串,所以我如果兩個字符串相同比它是迴文可以比較..錯誤類型轉換
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assg2_question1
{
class Program
{
static void Main(string[] args)
{
string word, reverse ="";
Console.WriteLine("type any word to check if it palindrome or not");
word = Console.ReadLine();
int lengthOfWord, i;
lengthOfWord = word.Length;
for(i=lengthOfWord ; i>=1 ; i--)
{
reverse = word[lengthOfWord];
}
}
}
}
我有兩個串字和反向,當我嘗試扭轉它顯示了字符串上反向錯誤=字[lengthOfWord];不能將char類型隱式轉換爲字符串,爲什麼?因爲我從來沒有在我的程序中使用字符
'reverse'是一個字符串變量,而'word [lengthOfword]'返回字符串中特定索引處的字符。所以這裏從char到string的轉換是不可能的。這就是錯誤所在。 –
你使用char。你的單詞[lengthOfWord]返回單個字符,它是一個字符。爲了更好地看到它,你可以做'char letter = word [lengthOfWord]; reverse = letter.ToString()' – FCin
[反轉字符串的最佳方法]的可能的副本(http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string) – ASh