我一直得到一个未处理的类型异常 'System.ArgumentOutOfRangeException Additional
信息:索引和长度必须指代字符串中的位置。
我已经尝试将数组设置为固定的amout但我一直遇到同样的问题
以下计划
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace encryptionApp
{
class Program
{
//function to chage substring to number
public static int ToNumber(string subString)
{
int returnInt;
// string to number convestion switch
switch (subString)
{
case "A":
returnInt = 1;
return returnInt;
case "B":
returnInt = 2;
return returnInt;
case "C":
returnInt = 3;
return returnInt;
case "D":
returnInt = 4;
return returnInt;
case "E":
returnInt = 5;
return returnInt;
case "F":
returnInt = 6;
return returnInt;
case "G":
returnInt = 7;
return returnInt;
case "H":
returnInt = 8;
return returnInt;
case "I":
returnInt = 9;
return returnInt;
case "J":
returnInt = 10;
return returnInt;
case "K":
returnInt = 11;
return returnInt;
case "L":
returnInt = 12;
return returnInt;
case "M":
returnInt = 13;
return returnInt;
case "N":
returnInt = 14;
return returnInt;
case "O":
returnInt = 15;
return returnInt;
case "P":
returnInt = 16;
return returnInt;
case "Q":
returnInt = 17;
return returnInt;
case "R":
returnInt = 18;
return returnInt;
case "S":
returnInt = 19;
return returnInt;
case "T":
returnInt = 20;
return returnInt;
case "U":
returnInt = 21;
return returnInt;
case "V":
returnInt = 22;
return returnInt;
case "W":
returnInt = 23;
return returnInt;
case "X":
returnInt = 24;
return returnInt;
case "Y":
returnInt = 25;
return returnInt;
case "Z":
returnInt = 26;
return returnInt;
case "1":
returnInt = 27;
return returnInt;
case "2":
returnInt = 28;
return returnInt;
case "3":
returnInt = 29;
return returnInt;
case "4":
returnInt = 30;
return returnInt;
case "5":
returnInt = 31;
return returnInt;
case "6":
returnInt = 32;
return returnInt;
case "7":
returnInt = 33;
return returnInt;
case "8":
returnInt = 34;
return returnInt;
case "9":
returnInt = 35;
return returnInt;
case "0":
returnInt = 36;
return returnInt;
case " ":
returnInt = 37;
return returnInt;
default:
subString = " ";
return 0;
}
}
//function to convrt back to string
public static string ToText(int subInt)
{
string subString;
// number to string conversion swith
switch (subInt)
{
case 1:
subString = "A";
return subString;
case 2:
subString = "B";
return subString;
case 3:
subString = "C";
return subString;
case 4:
subString = "D";
return subString;
case 5:
subString = "E";
return subString;
case 6:
subString = "F";
return subString;
case 7:
subString = "G";
return subString;
case 8:
subString = "H";
return subString;
case 9:
subString = "I";
return subString;
case 10:
subString = "J";
return subString;
case 11:
subString = "K";
return subString;
case 12:
subString = "L";
return subString;
case 13:
subString = "M";
return subString;
case 14:
subString = "N";
return subString;
case 15:
subString = "O";
return subString;
case 16:
subString = "P";
return subString;
case 17:
subString = "Q";
return subString;
case 18:
subString = "R";
return subString;
case 19:
subString = "S";
return subString;
case 20:
subString = "T";
return subString;
case 21:
subString = "U";
return subString;
case 22:
subString = "V";
return subString;
case 23:
subString = "W";
return subString;
case 24:
subString = "X";
return subString;
case 25:
subString = "W";
return subString;
case 26:
subString = "Z";
return subString;
case 27:
subString = "1";
return subString;
case 28:
subString = "2";
return subString;
case 29:
subString = "3";
return subString;
case 30:
subString = "4";
return subString;
case 31:
subString = "5";
return subString;
case 32:
subString = "6";
return subString;
case 33:
subString = "7";
return subString;
case 34:
subString = "8";
return subString;
case 35:
subString = "9";
return subString;
case 36:
subString = "0";
return subString;
case 37:
subString = " ";
return subString;
default:
subString = " ";
return subString;
}
}
static void Main(string[] args)
{
// my variubles
string message;
string encMessage = "";
int encKey;
int keyCount = 0;
int tempInt;
string userChoice;
//get user choice
Console.WriteLine("Press [1] to Encrypt a message\n"
+ "Press [2] to Decrypt a message");
userChoice = Console.ReadLine();
//ask user for message
Console.WriteLine("Plaeas enter the message");
message = Console.ReadLine();
// make array of ints and strings
int[] messageInt = new int[message.Length];
string[] messageString = new string[message.Length];
//ask user for Key
Console.WriteLine("Please enter Eyncrytion Key");
encKey = int.Parse(Console.ReadLine());
//start for loop to brake up message into ints
for (int i = 0; i <= message.Length; i++)
{
//set message to numbers
//THIS IS AS FAR AS THE PROGRAM QILL GET
tempInt = ToNumber(message.Substring(i, i));
// create array of chars
messageInt[i] = tempInt;
}
//loop to change numbers back to strings
for (int i = 0; i <= message.Length; i++)
{
// changenumber of message String array to ecnipt the message
messageInt[i] = messageInt[i] + ToNumber(encKey.ToString().Substring(keyCount, keyCount));
if (messageInt[i] > 37)
{
messageInt[i] = messageInt[i] - 37;
}
//use ToString function to chage number back to enyncripted letter
messageString[i] = ToText(messageInt[i]);
// if else statment used to set part of ecnKey that is used
if (keyCount < encKey.ToString().Length)
{
keyCount++;
}
else
{
keyCount = 0;
}
}
// combine string array to make ecnipted message
for (int i = 0; i <= message.Length; i++)
{
encMessage = encMessage + messageString[i];
}
Console.WriteLine(encMessage);
//pause App
Console.ReadLine();
}
}
}
答案 0 :(得分:1)
我相信这是错误:
int[] messageInt = new int[message.Length];
...
for (int i = 0; i <= message.Length; i++)
由于索引是基于0的,因此数组中最大的元素不会具有索引of the message.Length
,而是message.Length - 1
。要解决此问题,只需将其替换为:
for (int i = 0; i <= message.Length; i++)
用这个:
for (int i = 0; i < message.Length; i++)
答案 1 :(得分:0)
我会这样做
static List<string> convertArray = new List<string>() { "START", "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " " };
static void Main(string[] args)
{
}
public static int ToNumber(string subString)
{
int results = convertArray.IndexOf(subString);
if (results == -1) results = 0;
return results;
}
public static string ToText(int subInt)
{
string results = " ";
if (subInt > 0 && subInt < convertArray.Count())
{
results = convertArray[subInt];
}
return results;
}