如果输入无效,则验证输入并重复输入

时间:2013-09-28 18:12:01

标签: c# winforms

使用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);

                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。我只是按照您的代码并在方法中组织它们。

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;
    }
}

答案 1 :(得分:0)

这是一些伪代码,它们将继续接受用户输入,直到它们输入“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循环只会执行一次,这使得它们在这种情况下无用。