C#,整数不在两个数字之间输入

时间:2016-03-15 20:50:17

标签: c# arrays

当输入的整数不在0到10之间时,我必须输入它然后它将显示一条消息,无论输入的数字是否在0到10之间。同时当输入-99时它将退出程序。我尝试过while语句,似乎没有任何工作。

        int total = 0;
        double avg;            
        string inValue;

        int[] score = new int[8];

        // prompt user for initial values
        for (int i = 0; i < score.Length; i++)
        {
            Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
            inValue = ReadLine();
            if (int.TryParse(inValue, < 1 && > 10) == false)
                WriteLine("Integer entered, {0}, is not between 0 and 10.");
            if (int.TryParse(inValue, out score[i])
                == false)
                WriteLine("\n\tInvalid data - re-enter homework score: ");


        }

4 个答案:

答案 0 :(得分:1)

你不能把&#34;大于10,小于1&#34;条件里面 TryParse()方法,它不是support that。因此,请分别检查条件。也无需检查if (something == false),因为它与if (!something)相同。我更改了ReadLine / Write / WriteLine以使Console.前置,以便它可以在我的系统上运行。你需要一个while循环来获得&#34;请重新输入作业分数&#34;按照你的意愿工作,但这里的代码确实解决了你原来的问题..

        int total = 0;
        double avg;
        string inValue;

        int[] score = new int[8];

        // prompt user for initial values
        for (int i = 0; i < score.Length; i++)
        {
            Console.Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
            inValue = Console.ReadLine();
            if (int.TryParse(inValue, out score[i]))
            {
                if (score[i] == 99)
                { Environment.Exit(0); }

                bool between0and10 = score[i] <= 10 && score[i] >= 0;
                if (!between0and10)
                { Console.WriteLine("Integer entered, {0}, is not between 0 and 10."); }
            }
            else
            { Console.WriteLine("\n\tInvalid data - re - enter homework score: "); }
        }

答案 1 :(得分:0)

试试这个:

int total = 0;
double avg;
string inValue;

int[] score = new int[8];

// prompt user for initial values
for (int i = 0; i < score.Length; i++)
{
    Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
    inValue = ReadLine();
    if (!int.TryParse(inValue, out score[i]))
    {
        WriteLine("\n\tInvalid data, StackOverflow did your homework! - re-enter homework score: ");
    }
    else if (score[i] < 1 || score[i] > 10) 
    {
        WriteLine("Integer entered, {0}, is not between 0 and 10.");
    }
}

答案 2 :(得分:0)

int[] score = new int[8];
            string sVal;
            int val;
            int i = 0;
            while (i < score.Length)
            {
                Console.WriteLine("Please enter homework score [0 to 10] (-99 to exit):");
                sVal = Console.ReadLine();
                if (int.TryParse(sVal, out val))
                {
                    //if quit
                    if (val == -99)
                        break;

                    //if valid range
                    if (val >= 0 && val <= 10) 
                    {
                        score[i] = val;
                        i++;
                    }
                    else //invalid input range
                        Console.WriteLine("Invalid data - re-enter homework score:");
                }
                else //not a number
                    Console.WriteLine("Invalid data - re-enter homework score:");
            }

答案 3 :(得分:0)

int total = 0;
double avg;
int parsedScore;
//this bool will tell us if the data entered is valid
bool isValid;
int[] score = new int[8];
string inValue;
// prompt user for initial values
for (int i = 0; i < score.Length; i++)
    {
        Console.Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
        inValue = Console.ReadLine();
        //here we check that the data entered is valid and set our bool to the result
       isValid = int.TryParse(inValue, out parsedScore);
        if (isValid && parsedScore == -99) //check first if it is -99 and then exit if need be.
        {
            System.Environment.Exit(0);
        }
        //if it is not valid we are going to prompt them to re-enter their number 
        if(!isValid || (parsedScore < 0 && parsedScore > 10))
        {
            Console.WriteLine("Integer not entered or {0}, is not between 0 and 10.", inValue);
            i--; //we decrement i in order to let them re-enter at this index
        }
        else
        {
            //valid number, do logic here.
        }
相关问题