为了运行一个简单的代码,我一直收到错误 [英] I keep getting an error while tring to run a simple code
问题描述
我想在C#,visual studio 2015上运行我的代码,并不断收到错误
代码即可运行:
i am tring to run my code on C#, visual studio 2015, and keep getting an error
the code im tring to run:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int len;
string str="";
while (str != "end")
{
len = str.Length;
str.Remove(len - 1, 1);
str.Remove(len - 2, 1);
str.Remove(len - 3, 1);
str.Remove(len - 4, 1);
str.Remove(1, 1);
str.Remove(2, 1);
str.Remove(3, 1);
str.Remove(4, 1);
Console.WriteLine(str);
}
}
}
}
任何人都可以告诉我这是什么问题?
我尝试了什么:
我没有我真的尝试了什么,只是为了理解我能做什么
can anyone tell me what is the problem?
What I have tried:
I havn't tried anything really, just tring to anderstand what i can do
推荐答案
除了其他解决方案中有关空字符串和不可避免的参数超出范围异常的建议,您需要了解字符串不变,这意味着一旦创建,它们永不修改。
这意味着str.Remove(...,1)
不更改str
中的字符串,它返回一个 new 字符串,其中删除了指定的字符。因此,您必须将此值分配给str
以查看渐进式更改:
Besides the advice in the other Solutions about your empty string and the inevitable "Argument out of range" exception, you need to understand that strings are invariant, which means that once created, they are never modified.
This means thestr.Remove(..., 1)
does not change the string instr
, it returns a new string with the specified characters removed. So you must assign this value tostr
to see the progressive changes:
str = str.Remove(len - 1, 1);
str = str.Remove(len - 2, 1);
// etc. ...
那么......你究竟想要完成什么? (因为此代码中潜伏着许多其他潜在的问题...)
So... what are you actually trying to accomplish? (Since there are plenty of other potential problems lurking in this code...)
您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]
https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]
在运行程序行时检查str
line。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
Inspectstr
as you run the program line by line.
嗯...看看你的代码,以及你要求它做什么。使用调试器 - 它会帮助你理解。
当你到达循环时,你在str
中有什么?
Nothing - 一个空白(空)字符串。
所以而
测试将空字符串与固定字符串end进行比较并返回是的,因为它们不一样,它进入循环。
所以你得到字符串的长度。因为字符串是空的,它不包含任何字符,所以它的长度为零。
那么你告诉系统在字符串开头之前删除字符:
Well...look at your code, and what you are asking it to do. Use teh debugger - it'll help you understand.
What do you have instr
when you arrive at the loop?
Nothing - a blank (empty) string.
So thewhile
test compares an empty string with a fixed string "end" and returns true because they are not the same, and it enters the loop.
So you get the length of the string. And because the string is empty, it contains no characters, so it's length is zero.
So then you tell the system to remove the character before the start of the string:
str.Remove(len - 1, 1);
0 - 1, 1
-1, 1
这就是做什么?你无法删除来自那里的人物!并引发异常。可能会抛出Argument out of range异常,因为它试图将字符串作为数组访问,并且C#中的所有数组都以0开头 - 所以任何负数都是非法的数组索引。
你怎么做才能解决它?好吧,你可以检查字符串是否为空而不进入循环 - 或者你可以从某个地方读取一个字符串进行处理,并确保你要处理的内容能够正常工作......
And it goes "Do what? You can't remove characters from there!" and throws an exception. Probably, it throws an "Argument out of range" exception because it's trying to access the string as an array and all arrays in C# start with 0 - so any negative number is illegal as an array index.
What do you do to fix it? Well, you could check if the string is empty and not enter the loop - or you could read a string to process from somewhere and ensure that what you are trying to process will work...
这篇关于为了运行一个简单的代码,我一直收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!