C# 如何将输入字符串与数组中的索引相等
我试着做这个程序,当我把一个字母输入到一个输入字段时,输出文本在输入文本字母后显示字母,比如a应该是b,或者b应该是c,等等。所以我想我可以为字母表创建一个字符串数组,如果输入字母等于循环数组的索引,那么结果将等于索引+1。我试过了,它给了我和输入一样的东西。我错过什么了吗C# 如何将输入字符串与数组中的索引相等,c#,arrays,unity3d,C#,Arrays,Unity3d,我试着做这个程序,当我把一个字母输入到一个输入字段时,输出文本在输入文本字母后显示字母,比如a应该是b,或者b应该是c,等等。所以我想我可以为字母表创建一个字符串数组,如果输入字母等于循环数组的索引,那么结果将等于索引+1。我试过了,它给了我和输入一样的东西。我错过什么了吗 public class Encryption : MonoBehaviour { public InputField input; public Text output; string inputText; public
public class Encryption : MonoBehaviour
{
public InputField input;
public Text output;
string inputText;
public Toggle L337Toggle;
public Toggle CharShiftToggle;
public Toggle DoubleCaseToggle;
public Toggle VowelToggle;
void Start()
{
L337Toggle.isOn = false;
CharShiftToggle.isOn = false;
DoubleCaseToggle.isOn = false;
VowelToggle.isOn = false;
}
private void Update()
{
inputText = input.text;
output.text = inputText;
IEncryption _textEncryption = new TextEncryption(inputText);
IEncryption _L337Encryption = new L337Encryption(_textEncryption);
IEncryption _CharShiftEncryption = new CharShiftEncryption(_textEncryption);
if (L337Toggle.isOn == true)
{
output.text = _L337Encryption.Encrypt();
}
else if (CharShiftToggle.isOn == true)
{
output.text = _CharShiftEncryption.Encrypt();
}
}
public interface IEncryption
{
string Encrypt();
}
public class TextEncryption : IEncryption
{
private string originalString;
public TextEncryption(string original)
{
originalString = original;
}
public string Encrypt()
{
Debug.Log("Encrypting Text");
return originalString;
}
}
public class L337Encryption : IEncryption
{
private IEncryption _encryption;
public L337Encryption(IEncryption encryption)
{
_encryption = encryption;
}
public string Encrypt()
{
Debug.Log("Encrypting L337 Text");
string result = _encryption.Encrypt();
result = result.Replace('a', '4').Replace('b', '8').Replace('e', '3').Replace('g', '6').Replace('h', '4').Replace('l', '1')
.Replace('o', '0').Replace('q', '9').Replace('s', '5').Replace('t', '7').Replace('A', '4').Replace('B', '8')
.Replace('E', '3').Replace('G', '6').Replace('H', '4').Replace('L', '1')
.Replace('O', '0').Replace('Q', '9').Replace('S', '5').Replace('T', '7');
return result;
}
}
public class CharShiftEncryption : IEncryption
{
private IEncryption _encryption;
public CharShiftEncryption(IEncryption encryption)
{
_encryption = encryption;
}
public string Encrypt()
{
Debug.Log("Encrypting Character Shift Text");
string result = _encryption.Encrypt();
string[] Letters=new string[] { "a", "b'", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
for (int i=0;i<Letters.Length;i++)
{
if (Letters[i] == result)
{
result = Letters[i + 1];
if (result == Letters[25])
{
result = Letters[0];
}
}
}
return result;
}
}
我建议使用字典,而不是数组。首先创建它:
using System.Linq;
...
// {{'a', 'b'},
// {'b', 'c'},
// ...
// {'y', 'z'},
// {'z', 'a'}}
const int range = 'z' - 'a' + 1;
static Dictionary<char, char> s_Dict = Enumerable
.Range('a', range) // all letters enumerated
.Select(item => new {
key = (char) item,
value = (char) ('a' + (item - 'a' + 1) % range),
})
.ToDictionary(item => item.key,
item => item.value);
测试:
结果:
encrypt me, please!
fodszqu nf, qmfbtf!
一个天真的实现,没有立即使用Linq在unity中玩
using System.Collections.Generic;
using UnityEngine;
public class Encryption : MonoBehaviour {
public string testString = "Every lower case vowel gets replaced by the 'next' one.";
Dictionary<char, char> charMap;
string result = "";
void Start () {
charMap = new Dictionary<char, char>() {
{'a','e'},
{'e','i'},
{'i','o'},
{'o','u'},
{'u','a'}
};
result = "";
for (int i = 0; i < testString.Length; i++) {
result += charMap.ContainsKey(testString[i]) ? charMap[testString[i]] : testString[i];
}
Debug.Log(result);
}
}
你的if语句永远不会返回true。因为结果永远不会被设置,因为if语句永远不会返回true,所以它是一个圆。编辑:你没有得到一个错误,这是一个无限循环吗?刚刚添加了一个屏幕快照如果你通过z,正确的字母是什么?您也只检查小写字母。我知道,我要为大写字母创建另一个数组,如果索引>letters.length,那么我将设置I=0如果我希望源为用户输入,该怎么办?如果您希望源为yser输入-string source=Console.ReadLine;假设您希望console InputUnumerable在System.Linq namespaceOk的当前上下文中不存在。这很难翻译成我的代码。我只是在我的问题中添加了我程序的其余部分。我试图为我的加密对象创建一个decorator对象,就像我对L337 decorator对象所做的那样。我需要encrypt for character shift从文本加密继承。如果我不想使用测试字符串,并且我想使用用户在unity中输入的内容,该怎么办?在您的例子中,它将是_encryption.encrypt.的返回值,因此该行将为'for int I=0;i<\u Encryption.Encrypt.Length;i++?试试看,你的电脑不会爆炸如果不是的话,最糟糕的情况是你出错了,它不工作了,应该是_encryption.Encrypt.Length。还有两件事。首先,dmitry和我的上述答案的组合也适用于L337加密。第二,请勾选有助于正确回答的答案。哦,还有第三点,我对string+string的使用是次优的,并且会产生大量的垃圾,读入System.Text的StringBuilder类,这对于在运行时创建字符串更好。
encrypt me, please!
fodszqu nf, qmfbtf!
using System.Collections.Generic;
using UnityEngine;
public class Encryption : MonoBehaviour {
public string testString = "Every lower case vowel gets replaced by the 'next' one.";
Dictionary<char, char> charMap;
string result = "";
void Start () {
charMap = new Dictionary<char, char>() {
{'a','e'},
{'e','i'},
{'i','o'},
{'o','u'},
{'u','a'}
};
result = "";
for (int i = 0; i < testString.Length; i++) {
result += charMap.ContainsKey(testString[i]) ? charMap[testString[i]] : testString[i];
}
Debug.Log(result);
}
}