Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证输入,如果输入无效,则重复输入_C#_Winforms - Fatal编程技术网

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

C# 验证输入,如果输入无效,则重复输入,c#,winforms,C#,Winforms,使用VisualStudio,这是一个练习,我需要添加其他学生,这不是问题。我的主要问题是:如果包含的值大于100或小于0,我希望代码跳回输入问题,但我不知道检查(验证)用户输入的代码,任何帮助都将非常有用,我将不胜感激 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Student_Marks_For_Statement { class Pr

使用VisualStudio,这是一个练习,我需要添加其他学生,这不是问题。我的主要问题是:如果包含的值大于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;
    }
}
公共类程序
{
公共静态字符决策符号=“”;
公共静态双TotalMarkOfFirstStudent=0;
公共静态双TotalMarkOfSecondStudent=0;
公共静态双输入值=0;
公共静态void Main(字符串[]args)
{
Console.WriteLine(“输入学生1的分数:”);
while(DecisionSign!=“n”)
{
CalculateFirstStudentResult();
}
决策符号=“”;
Console.WriteLine(“输入学生2的分数:”);
while(DecisionSign!=“n”)
{
CalculateSecondStudentResult();
}
Console.WriteLine(“学生1总计:+TotalMarkOfFirstStudent”);
Console.WriteLine(“学生2总计:+TotalMarkOfSecondStudent”);
Console.ReadKey();
}
公共静态字符CalculateFirstStudentResult()
{
决策符号=“”;
Console.WriteLine(“输入值:”);
inputValue=Convert.ToDouble(Console.ReadLine());

如果(inputValue>=0&&inputValue=0&&inputValue以下是一些psuedo代码,它们将继续接受用户输入,直到输入“n”,并验证用户输入是否介于0和100之间(包括):

moredata=“y”
而moredata=“y”
提示得分
把分数换算成双倍
如果分数=100
告诉用户分数必须介于0和100之间
moredata=“y”
其他的
把分数加到总数上
如果结束
询问用户是否有更多数据
显示总分
结束时
while
循环将继续提示用户输入信息,直到用户输入“n”。如果分数小于0或大于100,它会告诉用户分数超出范围,并会提示用户再次输入数据。否则,它会将分数添加到总数中,显示正在运行的总数,然后询问用户是否有更多数据

我建议在转换时使用
double.TryParse
,以避免出现错误(例如,如果用户输入字符串怎么办?)


此外,for循环只执行一次,这使得它们在这种情况下毫无用处。

您能至少给您的问题一个描述性的标题吗?您需要帮助是显而易见的,但它没有告诉我们您将要问的问题。使用方法如何?如果您懒惰,您可以使用“goto”但这是非常糟糕的做法。这是我第一次使用此网站,因此我为标题中缺少详细信息表示歉意。您希望跳转到哪一行?我希望它跳回输入问题,因此当值被选中且高于100或低于0时,它将返回到输入问题并显示一条消息,说明invalid,我可以使用if语句,但我目前不知道如何检查输入是否在范围内。只需查看您建议的代码,对我来说很好,谢谢,我会看看它是如何执行的。在按下学生2上的按钮后,代码似乎崩溃了,而且,只是指出,“更多数据”问题出现在第一次输入之后,因此用户可以添加更多内容(如果是),或者将输入的值留给下一个学生。这不是最佳解决方案。您可以将其划分为类和更多方法。只需记住,最好将代码分为可能的部分。让人能够理解。对不起,我没有检查了它,我有一个想法,但它没有工作不幸的是,它必须在我的编程风格,所以它看起来是合法的,基本上我只是想弄清楚,在一个非常基本的,初学者的水平,在一个if语句中,如果输入的值低于或高于所需的值,它不会存储该值,它会重新循环该特定的方法程序也有同样的功能。如果值>=0,或者我们将在几周内学习psuedo代码,我想,对于我第一次尝试的循环,我只是想运行一次循环来获取数据,推导出运行总数,但我试图找出或获得帮助,如何在第一次输入时强制用户输入另一个数字t超出了范围。读了几遍psuedo代码后,它完全按照我的要求完成了,谢谢你,蒂姆。@RayneBlackham-不客气。祝你在课堂上好运,编程愉快!
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