验证输入,如果输入无效,则重复输入
本文关键字:输入 如果 验证 无效 | 更新日期: 2023-09-27 18:33:06
使用Visual Studio,这是一个练习,我需要添加其他学生,这不是问题。我的主要问题是:如果包含的值高于 100 或低于 0,我希望代码跳回到输入问题,但我不知道检查(验证)用户输入的代码,任何帮助都会非常有帮助,我将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student_Marks_For_Statement
{
class Program
{
static void Main(string[] args)
{
char moreData;
double total = 0;
double secondTotal = 0;
for (double student1 = 0; student1 == 0; student1++)
{
Console.Write("Enter mark for student 1: ");
student1 = Convert.ToDouble(Console.ReadLine());
total += student1;
Console.WriteLine("Any more data? Enter 'y' or 'n' then return");
moreData = Convert.ToChar(Console.ReadLine());
if (moreData == 'n')
{
;
}
if (moreData == 'y')
{
Console.Write("Enter Value :");
secondTotal = Convert.ToDouble(Console.ReadLine());
total += secondTotal;
}
for (double student2 = 0; student2 == 0; student2++)
{
Console.Write("Enter mark for student 2: ");
student2 = Convert.ToDouble(Console.ReadLine());
total += student2;
student2++;
Console.WriteLine("Any more data? Enter 'y' or 'n' then return");
moreData = Convert.ToChar(Console.ReadLine());
if (moreData == 'n')
{
;
}
if (moreData == 'y')
{
Console.Write("Enter Value :");
secondTotal = Convert.ToDouble(Console.ReadLine());
total += secondTotal;
student2++;
} Console.WriteLine("Total marks = : {0}", total);
}
}
}
}
}
你可以尝试这样的事情。我只是遵循您的代码并在方法中组织它们。
public class Program
{
public static char DecisionSign = ' ';
public static double TotalMarkOfFirstStudent = 0;
public static double TotalMarkOfSecondStudent = 0;
public static double inputValue = 0;
public static void Main(string[] args)
{
Console.WriteLine("Enter mark for student 1: ");
while (DecisionSign != 'n')
{
CalculateFirstStudentResult();
}
DecisionSign=' ';
Console.WriteLine("Enter mark for student 2: ");
while (DecisionSign != 'n')
{
CalculateSecondStudentResult();
}
Console.WriteLine("Student 1 got total : " + TotalMarkOfFirstStudent);
Console.WriteLine("Student 2 got total : " + TotalMarkOfSecondStudent);
Console.ReadKey();
}
public static char CalculateFirstStudentResult()
{
DecisionSign = ' ';
Console.WriteLine("Enter Value : ");
inputValue = Convert.ToDouble(Console.ReadLine());
if (inputValue >= 0 && inputValue <= 100)
{
TotalMarkOfFirstStudent += inputValue;
Console.WriteLine("Any more data? Enter 'y' or 'n' then return");
DecisionSign = Convert.ToChar(Console.ReadLine());
}
else
{
CalculateFirstStudentResult();
}
return DecisionSign;
}
public static char CalculateSecondStudentResult()
{
DecisionSign = ' ';
Console.WriteLine("Enter Value : ");
inputValue = Convert.ToDouble(Console.ReadLine());
if (inputValue >= 0 && inputValue <= 100)
{
TotalMarkOfSecondStudent += inputValue;
Console.WriteLine("Any more data? Enter 'y' or 'n' then return");
DecisionSign = Convert.ToChar(Console.ReadLine());
}
else
{
CalculateSecondStudentResult();
}
return DecisionSign;
}
}
下面是一些伪代码,它们将继续接受用户输入,直到他们输入"n",并验证用户输入是否介于 0 和 100(含)之间:
moredata = "y"
while moredata = "y"
Prompt for score
Convert score to double
if score <= 0 or score >= 100
Tell user score must be betwen 0 and 100
moredata = "y"
else
Add score to total
end if
ask user if they have more data
show total score
end while
while
循环将继续提示用户输入信息,直到他们输入"n"。如果分数小于 0 或大于 100,它将告诉用户分数超出范围,并提示他们再次输入数据。 否则,它会将分数添加到总计中,显示运行总计,然后询问用户是否有更多数据。
我建议在转换中使用double.TryParse
以避免错误(例如,如果用户输入字符串怎么办?
此外,您的 for 循环将只执行一次,这使得它们在这种情况下没有用处。