C# 我可以使用For循环给出不同的结果/值吗?
示例代码(错误,但为参考):C# 我可以使用For循环给出不同的结果/值吗?,c#,C#,示例代码(错误,但为参考): for(int i=0;ix i%3==1-->y i%3==2-->z 有很多方法可以满足您的需求: 提示:要测试其中任何一个,您可以使用,只需复制并粘贴上面的一个代码示例,然后点击F5运行,您将看到输出与您想要的匹配 使用数组: void Main() { 字符串[]字母=新的[]{“x”、“y”、“z”}; 对于(int-index=0;indexz 有很多方法可以满足您的需求: 提示:要测试其中任何一个,您可以使用,只需复制并粘贴上面的一个代码示例,然后
for(int i=0;i<10;i++)
{
如果((i%3)==0)
{
控制台写入线(i+“:x”);
}
如果((i%2)==0,则为else
{
控制台写入线(i+“:y”);
}
其他的
{
控制台写入线(i+“:z”);
}
}
我想要的结果是:
1:x
2:y
3:z
4:x
5:y
6:z
7:x
8:y
9:z
10:x
1:x
2:y
3:z
4:x
5:y
6:z
7:x
8:y
9:z
10:x
你想要
for (int i = 1; i <= 10; i++)
{
if ((i % 3) == 1)
{
Console.WriteLine(i+":x");
}
else if ((i % 3) == 2)
{
Console.WriteLine(i+":y");
}
else //if ((i % 3) == 0)
{
Console.WriteLine(i+":z");
}
}
for(inti=1;i您想要
for (int i = 1; i <= 10; i++)
{
if ((i % 3) == 1)
{
Console.WriteLine(i+":x");
}
else if ((i % 3) == 2)
{
Console.WriteLine(i+":y");
}
else //if ((i % 3) == 0)
{
Console.WriteLine(i+":z");
}
}
对于(int i=1;i这里的重要部分是不要更改计算模数的数值
换句话说,与其先检查i%3==0
,然后再检查i%2==0
,不如检查:
i%3==0
-->x
i%3==1
-->y
i%3==2
-->z
有很多方法可以满足您的需求:
提示:要测试其中任何一个,您可以使用,只需复制并粘贴上面的一个代码示例,然后点击F5运行,您将看到输出与您想要的匹配
使用数组:
void Main()
{
字符串[]字母=新的[]{“x”、“y”、“z”};
对于(int-index=0;index<10;index++)
{
Console.WriteLine((索引+1)+“:”+字母[索引%3]);
}
}
带开关/箱子
void Main()
{
对于(int-index=0;index<10;index++)
{
交换机(索引%3)
{
案例0:
控制台写入线((索引+1)+“:x”);
打破
案例1:
控制台写入线((索引+1)+“:y”);
打破
案例2:
控制台写入线((索引+1)+“:z”);
打破
}
}
}
带if语句
void Main()
{
对于(int-index=0;index<10;index++)
{
如果(索引%3==0)
控制台写入线((索引+1)+“:x”);
else if(索引%3==1)
控制台写入线((索引+1)+“:y”);
其他的
控制台写入线((索引+1)+“:z”);
}
}
这里重要的一点是不要更改计算模数的数值
换句话说,与其先检查i%3==0
,然后再检查i%2==0
,不如检查:
i%3==0
-->x
i%3==1
-->y
i%3==2
-->z
有很多方法可以满足您的需求:
提示:要测试其中任何一个,您可以使用,只需复制并粘贴上面的一个代码示例,然后点击F5运行,您将看到输出与您想要的匹配
使用数组:
void Main()
{
字符串[]字母=新的[]{“x”、“y”、“z”};
对于(int-index=0;index<10;index++)
{
Console.WriteLine((索引+1)+“:”+字母[索引%3]);
}
}
带开关/箱子
void Main()
{
对于(int-index=0;index<10;index++)
{
交换机(索引%3)
{
案例0:
控制台写入线((索引+1)+“:x”);
打破
案例1:
控制台写入线((索引+1)+“:y”);
打破
案例2:
控制台写入线((索引+1)+“:z”);
打破
}
}
}
带if语句
void Main()
{
对于(int-index=0;index<10;index++)
{
如果(索引%3==0)
控制台写入线((索引+1)+“:x”);
else if(索引%3==1)
控制台写入线((索引+1)+“:y”);
其他的
控制台写入线((索引+1)+“:z”);
}
}
vard=新字典{{1,“x”},{2,“y”},{0,“z”};
对于(inti=1;ivard=newdictionary{{1,“x”},{2,“y”},{0,“z”};
对于(int i=1;iYes,你绝对可以这样做-尽管你需要修改当前代码的一些部分。很难说出你在这里真正想要什么…是的,你绝对可以这样做-尽管你需要修改当前代码的一些部分。很难说出你在这里真正想要什么…是的,这就是我想要的谢谢!MattNote这些示例将以0开始,以9结束,这里的第一个代码示例与您问题中的原始代码具有相同的缺陷,第二个writeline调用缺少索引变量,因此代码不合法。感谢您指出Lasse-我已按照建议修复了它-并对您的答案进行了投票,因为它比我的答案更完整是的,这就是我想要的Thankz!Matt请注意,这些示例将以0开始,以9结束,这里的第一个代码示例与您问题中的原始代码具有相同的缺陷,第二个writeline调用缺少索引变量,因此代码不合法。感谢您指出Lasse-我已按照建议进行了修复-并对您的a进行了升级回答,因为它比我的更完整。谢谢马塞洛!!它对我有用。谢谢马塞洛!!它对我有用。
for (int i = 0; i < 10; i++)
{
switch (i%3)
{
case 0:
Console.WriteLine(i+":z");
break;
case 1:
Console.WriteLine(i+":x");
break;
case 2:
Console.WriteLine(i+":y");
break;
}
}
switch (i%3)
{
case 0: // :z
case 1: // :x
case 2: // :y
}
void Main()
{
string[] letters = new[] { "x", "y", "z" };
for (int index = 0; index < 10; index++)
{
Console.WriteLine((index + 1) + ":" + letters[index % 3]);
}
}
void Main()
{
for (int index = 0; index < 10; index++)
{
switch (index % 3)
{
case 0:
Console.WriteLine((index + 1) + ":x");
break;
case 1:
Console.WriteLine((index + 1) + ":y");
break;
case 2:
Console.WriteLine((index + 1) + ":z");
break;
}
}
}
void Main()
{
for (int index = 0; index < 10; index++)
{
if (index % 3 == 0)
Console.WriteLine((index + 1) + ":x");
else if (index % 3 == 1)
Console.WriteLine((index + 1) + ":y");
else
Console.WriteLine((index + 1) + ":z");
}
}
var d = new Dictionary<int,string> { {1,"x"}, {2,"y"}, {0,"z"} };
for(int i = 1; i<= 10; i++)
Console.WriteLine("{0}:{1}", i, d[i%3]);