我想将这两个循环合并为一个:
for (int i = 7; i <=100; i+=10)
{
Console.WriteLine(i);
}
for (int i = 0; i < 100; i+=7)
{
Console.WriteLine(i);
}
答案 0 :(得分:2)
for (int i = 0; i <=100; i++) {
if(i%7 == 0 || i%10 == 0)
Console.WriteLine (i);
}
或使用LINQ:
Enumerable.Range(0,100).Where(i => i%7 == 0 || i%10 == 0).ToList()
.ForEach(i=> {Console.WriteLine (i); });
如果您还想要重复的结果:
for (int i = 0; i <=100; i++) {
if(i%7 == 0) Console.WriteLine (i);
if(i%10 == 0 && i!=0) Console.WriteLine (i);
}
Enumerable.Range(7,100)
.Where(i => i%10 == 0).ToList().AddRange(
Enumerable.Range(0,100)
.Where(i => i%7 == 0).ToList())
.ForEach(i=> {Console.WriteLine (i); });
答案 1 :(得分:1)
这听起来很愚蠢,但它完全符合您的要求。它看起来不整洁,因为它保留了打印值的顺序,就像你的例子一样:
bool flag = false;
int factor = 10;
for (int i = 7; i < 100;)
{
Console.WriteLine(i);
i += factor;
if ( i >= 100)
{
if (flag)
break;
flag = true;
factor = 7;
i = 0;
}
}
它使用布尔标志和动态因子来完成在一个循环中传递的两个。
答案 2 :(得分:0)
我认为这是你要做的事情
<table cellpadding="5" cellspacing="0" border="0" style="width: 100%; border-collapse: collapse">
<tr>
<td>
<table cellpadding="2" cellspacing="0" style="border-collapse: collapse; text-align:center; width:500px;">
<tr>
<td>
<b>Insert HTML Code to Encrypt</b>
</td>
</tr>
<tr>
<td>
<textarea type="text" name="inputdata" value="" style="width:98%; height:120px" ></textarea>
</td>
</tr>
<tr>
<td>
<input type="button" value="Encrypt" onclick="OnSubmitPluginInput(this,'http://www.webtoolhub.com/plugins/wt561359-html-encrypter.aspx');" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<iframe name="pluginframe561359" frameborder="0" style="width: 100%; height: 300px"></iframe>
</td>
</tr>
<tr>
<td style="font-size: 9pt; font-family: Verdana, Arial;">
Powered by: <a href="http://www.webtoolhub.com/tn561359-html-encrypter.aspx" title="Free Webmaster Tools">WebToolHub.com</a>
</td>
</tr>
</table>
<script type="text/javascript" src="https://secure.webtoolhub.com/plugin.axd"></script>
答案 3 :(得分:-1)
我不知道你到底想要做什么,但如果我和我想要打印数字我会使用数学公式
var i = 0;
while(true)
{
var x = 7 + (i * 10);
if(x < 100)
Console.WriteLine(x);
var y = i * 7;
Console.WriteLine(y);
if(y >= 100)
break;
i++;
}