首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >验证输入,如果输入无效则重复输入

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

Stack Overflow用户
提问于 2013-09-29 02:12:01
回答 2查看 960关注 0票数 0

使用Visual Studio,这是一个练习,我需要添加其他学生,这不是问题。我的主要问题是:如果值大于100或小于0,我希望代码跳回输入问题,但我不知道检查(验证)用户输入的代码,任何帮助都会非常有帮助,我将非常感激。

代码语言:javascript
复制
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);

                }
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-09-29 02:55:28

你可以试试这样的东西。我只是遵循了您的代码,并在方法中组织了它们。

代码语言:javascript
复制
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;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-09-29 03:04:12

下面是一些伪代码,它们将继续接受用户输入,直到他们输入"n",并验证用户输入是否在0到100之间(包括0和100):

代码语言:javascript
复制
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循环将只执行一次,这使得它们在此场景中没有用处。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19070137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档