c# - Console.Redline 读取第二行
问题描述
我有一个简单的程序,告诉用户输入n个学生,为每个学生分配x个钱。最后,程序将x除以n,这意味着总钱由学生平均分配。
问题是Console.Readline()
正在读取第二个输入值,如下所示:
这意味着用户必须输入两次值,每次都Console.Readline()
被调用,这显然是错误的!
代码:
static void Main(string[] args)
{
double total = 0;
int noOfStudents = 0;
try
{
Console.WriteLine("Please enter the number of students :");
noOfStudents = checkInputTypeInt(Console.ReadLine());
Console.WriteLine("Enter the students(" + noOfStudents + ") money!");
for (int i = 0; i <= noOfStudents - 1; i++)
{
double money = checkInputTypeDouble(Console.ReadLine());
total += money;
}
double dividedTotal = total / noOfStudents;
Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
Console.ReadKey();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
private static int checkInputTypeInt(string s)
{
int numericValue = 0;
bool done = false;
while (!done)
{
if (!int.TryParse(Console.ReadLine(), out numericValue))
Console.WriteLine("The input must be between 0 and 1000!");
else if (numericValue > 100)
Console.WriteLine("The input must be between 0 and 1000!");
else
done = true;
}
return numericValue;
}
解决方案
你读了两次:
noOfStudents = checkInputTypeInt(Console.ReadLine());
并在checkInputTypeInt
方法中:
if (!int.TryParse(Console.ReadLine(), out numericValue))
您应该只将字符串发送到这样的方法:
noOfStudents = checkInputTypeInt(Console.ReadLine());
并在您的方法中检查该值,例如:
if (!int.TryParse(s, out numericValue))
在这种情况下,您的 main 方法中应该有 while 循环。
或者只是在被调用的方法中使用 readline 。
Console.WriteLine("Please enter the number of students :");
noOfStudents = checkInputTypeInt(); // and you should change your method to fit no arguments.
编辑:最终代码:
static void Main(string[] args)
{
double total = 0;
int noOfStudents = 0;
try
{
Console.WriteLine("Please enter the number of students :");
noOfStudents = checkInputTypeInt();
Console.WriteLine("Enter the students(" + noOfStudents + ") money!");
for (int i = 0; i <= noOfStudents - 1; i++)
{
double money = checkInputTypeDouble(Console.ReadLine());
total += money;
}
double dividedTotal = total / noOfStudents;
Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
Console.ReadKey();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
private static int checkInputTypeInt()
{
int numericValue = 0;
bool done = false;
while (!done)
{
if (!int.TryParse(Console.ReadLine(), out numericValue))
Console.WriteLine("The input must be between 0 and 1000!");
else if (numericValue > 100)
Console.WriteLine("The input must be between 0 and 1000!");
else
done = true;
}
return numericValue;
}
或者:
static void Main(string[] args)
{
double total = 0;
int noOfStudents = -1;
try
{
Console.WriteLine("Please enter the number of students :");
do{
noOfStudents = checkInputTypeInt(Console.ReadLine());
}while(noOfStudents == -1);
Console.WriteLine("Enter the students(" + noOfStudents + ") money!");
for (int i = 0; i <= noOfStudents - 1; i++)
{
double money = checkInputTypeDouble(Console.ReadLine());
total += money;
}
double dividedTotal = total / noOfStudents;
Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
Console.ReadKey();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
private static int checkInputTypeInt(string s)
{
int numericValue = -1;
if (!int.TryParse(Console.ReadLine(), out numericValue))
Console.WriteLine("The input must be between 0 and 1000!");
else if (numericValue > 1000)
{Console.WriteLine("The input must be between 0 and 1000!");
numericValue = -1;
}
return numericValue;
}
推荐阅读
- python - 如何从熊猫数据框中的日期获取澳大利亚财政年度
- python - django 无法将请求正文保存到 sqlitedb
- flutter - 对话框中提升按钮的设置状态(颤振)?
- ruby-on-rails - 将 Rails 版本从 5.1.3 升级到 5.2.3 后 PgQuery gem 无法正常工作
- flutter - Flutter:从谷歌控制台/播放安装时未应用主题
- google-earth-engine - Google 地球引擎 - 错误“无法导出阵列波段。” 将文件从资产导出到驱动器作为 Geotiff 时?
- cpanel - 如何修复我的主页显示 403 错误?
- api - 电话会议和视频广播
- node.js - 多个 API 调用与一个 API 调用(但使用表连接)
- python - 如何在保持自定义返回值的同时在模拟类上调用 assert_call_with?