我正在尝试制作一个程序,用于根据用户给出的数字计算某些特定数据.在这个例子中,我的程序计算范围(10,103)中可被2整除的数字量,以及范围(15,50)中的数字量,在用户给出的数字内可被3整除.在这个阶段,我的程序给出结果,当给出10个数字时(正如我在循环中指定的那样).当用户输入空行时,无论是否输入5或100个数字,如何让我的程序停止读取数字并给出结果?
这是我的代码,因为它现在寻找:
using System;
namespace Program1
{
class MainClass
{
public static void Main (string[] args)
{
int input10_103_div_2 = 0;
int input15_50_div_3 = 0;
for (int i = 0; i < 10; i++)
{
string input = Console.ReadLine ();
double xinput = double.Parse (input);
if (xinput > 10 && xinput <= 103 && (xinput % 2) == 0)
{
input10_103_div_2++;
}
if (xinput > 15 && xinput < 50 && (xinput % 3) == 0)
{
input15_50_div_3++;
}
}
Console.WriteLine ("Amount of numbers in range (10,103) divisible by 2: " + input10_103_div_2);
Console.WriteLine ("Amount of numbers in range (15,50) divisible by 3: " + input15_50_div_3);
}
}
}
Run Code Online (Sandbox Code Playgroud)
而不是,做:
string input = Console.ReadLine();
while(input != String.Empty)
{
//do things
input = Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
如果你试图允许任何数量的输入.要么
if(input == "")
break;
Run Code Online (Sandbox Code Playgroud)
如果你想要for循环