C#如何逃离两行循环
这是我的代码:C#如何逃离两行循环,c#,loops,C#,Loops,这是我的代码: while(true){ for(int x = 0; x < 10; x++){ StringArray[x] = new string(); if(isDead){ break; //break out of while loop also } } } while(true){ 对于(int x=0;x
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
break; //break out of while loop also
}
}
}
while(true){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
break;//同时中断while循环
}
}
}
我该怎么做呢?对不起,如果我的英语很好,我还在学习。你可以创建一个bool,例如:
bool leaveLoop;
如果isDead为true,则将leaveLoop设置为true,在while循环中,然后检查leaveLoop是否为true以断开它。您可以创建一个bool,例如:
bool leaveLoop;
如果isDead为true,则将leaveLoop设置为true,在while循环中,然后检查leaveLoop是否为true以断开它。将while循环更改为变量,然后将该变量设置为false(例如,
isDead
变量)
while(!isDead){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
break;//同时中断while循环
}
}
}
这样,您的
break
将退出for
循环,然后将isDead
设置为true将在执行循环时停止。将while循环更改为变量,然后将该变量设置为false(例如,您的isDead
变量)
while(!isDead){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
break;//同时中断while循环
}
}
}
这样,您的中断将使您退出for
循环,然后将isDead设置为true将在执行循环时停止。尝试以下操作:
bool bKeepRunning = true;
while(bKeepRunning){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
bKeepRunning = false;
break;
}
}
}
bool bKeepRunning=true;
同时(运行时){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
bKeepRunning=false;
打破
}
}
}
尝试以下操作:
bool bKeepRunning = true;
while(bKeepRunning){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
bKeepRunning = false;
break;
}
}
}
bool bKeepRunning=true;
同时(运行时){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
bKeepRunning=false;
打破
}
}
}
创建一个内联函数,并调用它。使用lambda中的return
var mt = () => {
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
return
}
}
}
}
mt();
var mt=()=>{
while(true){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
返回
}
}
}
}
mt();
创建一个内联函数,并调用它。使用lambda中的return
var mt = () => {
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
return
}
}
}
}
mt();
var mt=()=>{
while(true){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
返回
}
}
}
}
mt();
据我所知,您希望在一个条件下中断两个循环。您可以执行以下操作
bool DirtyBool = true;
while(DirtyBool)
{
for(int x = 0; x < 10; x++)
{
StringArray[x] = new string();
if(isDead)
{
DirtyBool = false;
break; //break out of while loop also
}
}
}
bool DirtyBool=true;
while(DirtyBool)
{
对于(int x=0;x<10;x++)
{
StringArray[x]=新字符串();
如果(isDead)
{
DirtyBool=假;
break;//同时中断while循环
}
}
}
据我所知,您希望在一个条件下中断两个循环。您可以执行以下操作
bool DirtyBool = true;
while(DirtyBool)
{
for(int x = 0; x < 10; x++)
{
StringArray[x] = new string();
if(isDead)
{
DirtyBool = false;
break; //break out of while loop also
}
}
}
bool DirtyBool=true;
while(DirtyBool)
{
对于(int x=0;x<10;x++)
{
StringArray[x]=新字符串();
如果(isDead)
{
DirtyBool=假;
break;//同时中断while循环
}
}
}
对于这种情况,我最喜欢的方法是将代码移动到一个单独的例程中,并在需要中断时直接从中返回。两个循环的复杂性与我喜欢在一个例程中包含的一样大。对于这种情况,我最喜欢的方法是将代码移动到一个单独的例程中,并在需要中断时直接从中返回。两个循环的复杂性与我希望在一个例程中包含的一样大。您可以使用goto
:
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
goto EndOfWhile; //break out of while loop also
}
}
}
EndOfWhile: (continue your code here)
while(true){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
goto EndOfWhile;//同时中断while循环
}
}
}
EndOfWhile:(在此处继续您的代码)
您可以使用goto
:
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
goto EndOfWhile; //break out of while loop also
}
}
}
EndOfWhile: (continue your code here)
while(true){
对于(int x=0;x<10;x++){
StringArray[x]=新字符串();
如果(isDead){
goto EndOfWhile;//同时中断while循环
}
}
}
EndOfWhile:(在此处继续您的代码)
你可以将x=2
改为0
,你应该提供更多关于何时打破循环的信息??可能的重复请提交所有代码,这还不足以完全理解发生了什么。很可能这是一个重复,我认为@Houseman是对的。你可以随时使用备受诟病的goto
声明。你可以用x=2
代替0
,并且你应该提供更多关于何时打破循环的信息??可能的重复请提交所有代码,这还不足以完全理解发生了什么。这很可能是重复的,我认为@Houseman是对的。你可以随时使用备受诽谤的goto
语句