0

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

                }
            }
        }
    }
}
4

2 回答 2

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;
    }
}
于 2013-09-28T18:55:28.040 回答
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.TryParseon 转换以避免错误(例如,如果用户输入字符串怎么办?)。

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

于 2013-09-28T19:04:11.937 回答