Read() only takes first input but no other input from other Console.Read()

时间:2016-08-31 17:43:40

标签: c#

I am following instructions in a C# Tutorial video that is great. I am building a file with the notes and code from the video.

I reviewed similar questions but they do not solve this problem.

Here is a copy of the CS file:

        static void Main(string[] args)
        {
            // Single line comments
            /* test multi-line comments
             * asldkjasldkjaskd
             * asldkjasldkja
             * alsdkjalksdj
             * */
            Console.WriteLine("Hello world!");
            Console.Write("What is your name? ");
            string name = Console.ReadLine();
            Console.WriteLine("Hello " + name);

            bool canVote = true;

            char grade = 'A';

            // Integer with a max number of 2,147,483,647
            int maxInt = int.MaxValue;

            //Long with max value of 9,223,372,036,854,775,807
            long maxLong = long.MaxValue;

            // Decimal has a max value of 79,228,162,514,264,337,593,543,950,335
            // If you need something bigger, look up BigInteger
            decimal maxDecimal = decimal.MaxValue;

            // A float is a 32 bit number with a max value of 3.402823E+38 with 7 decimal positions
            float maxFloat = float.MaxValue;

            // A double is a 32 bit number with a max value of 1.797693134E+308 with 15 decimal positions
            double maxDouble = double.MaxValue;

            Console.WriteLine("Max Int : " + maxInt);
            Console.WriteLine("Max Long : " + maxLong);
            Console.WriteLine("Max Decimal : " + maxDecimal);
            Console.WriteLine("Max Float : " + maxFloat);
            Console.WriteLine("Max Double : " + maxDouble);

            var anotherName = "Tom";
            // anotherName = 2; Cannot implicitly convert a gype 'int' to a 'string' 

            Console.WriteLine("anotherName is a {0}", anotherName.GetTypeCode());

            // Math

            Console.WriteLine("5 + 3 = " + (5 + 3));
            Console.WriteLine("5 - 3 = " + (5 - 3));
            Console.WriteLine("5 * 3 = " + (5 * 3));
            Console.WriteLine("5 / 3 = " + (5 / 3));
            Console.WriteLine("5.2 % 3 = " + (5.2 % 3));

            int i = 0;

            Console.WriteLine("i++ = " + (i++));
            Console.WriteLine("++i = " + (++i));
            Console.WriteLine("i-- = " + (i--));
            Console.WriteLine("--i = " + (--i));

            Console.WriteLine("i +- 3 = " + (i +- 3));
            Console.WriteLine("i -= 2 = " + (i -= 2));
            Console.WriteLine("i *= 2 = " + (i *= 2));
            Console.WriteLine("i /= 2 = " + (i /= 2));
            Console.WriteLine("i %= 2 = " + (i %= 2));

            // casting

            // if no magnitude is lost, casting will happen automatically. But otherwise, you set it up as follows

            double pi = 3.14;
                int intPi = (int)pi;
            Console.WriteLine("intPi = " + intPi);

            // Math functions
            // Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Sin, Sinh, Tan, Tanh

            double number1 = 10.5;
            double number2 = 15;

            Console.WriteLine("number1 is " + number1);
            Console.WriteLine("number2 is " + number2);

            Console.WriteLine("Math.Abs(number1) " + (Math.Abs(number1)));
            Console.WriteLine("Math.Ceiling(number2) " + (Math.Ceiling(number1)));
            Console.WriteLine("Math.Floor(number1) " + (Math.Floor(number1)));
            Console.WriteLine("Math.Max(number1,number2) " + (Math.Max(number1,number2)));
            Console.WriteLine("Math.Min(number1,number2) " + (Math.Min(number1,number2)));
            Console.WriteLine("Math.Pow(number1, 2) " + (Math.Pow(number1, 2)));
            Console.WriteLine("Math.Round(number1) " + (Math.Round(number1)));
            Console.WriteLine("Math.Sqrt(number1) " + (Math.Sqrt(number1)));

            // random numbers

            Random rand = new Random();
            Console.WriteLine("Random number between 1 and 10 is " + rand.Next(1, 11));

            // Relational operators : > < >= <= == !=
            // Logical operators : && || ^ !  
            // note: ^ is the exclusive or

            Console.WriteLine("What is your child's age? (enter 0 - 18)");
            int age = Console.Read();

            if ((age >= 5) && (age <= 7))
            {
                Console.WriteLine("Go to Elementary School");
            } else if ((age > 7) && (age <= 13))
            {
                Console.WriteLine("Go to middle school");
            } else if ((age < 5) || (age > 13))
            {
                Console.WriteLine("Your child does not meet our age requirements.");
            } else
            {
                Console.WriteLine("Go to high school");
            }


            Console.WriteLine("What is your age? ");
            int workingAge = Console.Read();

            if ((workingAge < 14) || (workingAge > 67))
            {
                Console.WriteLine("You shouldn't work.");
            }

       }

The program ignores the input from the following:

            Console.WriteLine("What is your age? ");
            int workingAge = Console.Read();

The output is:

What is your age?
You shouldn't work.

So, the program is not stopping for my input, but rather seems to process its conditions based on the previous integer input value of 2 or 5.

Other articles talked about doing the following which I tried with no avail:

Console.WriteLine("What is your age? ");
int workingAge = Convert.ToInt32(Console.Read());

And

Console.WriteLine("What is your age? ");
int workingAge = int32.Parse(Console.Read());

The second one produced an error in Visual Studio The name 'int32' does not exist in the current context

I updated the script to use int.Parse(Console.ReadLine()), and it worked on Visual Studio 2007, but I ran this on Visual Studio 2015 Community Edition and it is doing the same exact thing as if the changes had no effect:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // Single line comments
            /* test multi-line comments
             * asldkjasldkjaskd
             * asldkjasldkja
             * alsdkjalksdj
             * */
            Console.WriteLine("Hello world!");
            Console.Write("What is your name? ");
            string name = Console.ReadLine();
            Console.WriteLine("Hello " + name);

            bool canVote = true;

            char grade = 'A';

            // Integer with a max number of 2,147,483,647
            int maxInt = int.MaxValue;

            //Long with max value of 9,223,372,036,854,775,807
            long maxLong = long.MaxValue;

            // Decimal has a max value of 79,228,162,514,264,337,593,543,950,335
            // If you need something bigger, look up BigInteger
            decimal maxDecimal = decimal.MaxValue;

            // A float is a 32 bit number with a max value of 3.402823E+38 with 7 decimal positions
            float maxFloat = float.MaxValue;

            // A double is a 32 bit number with a max value of 1.797693134E+308 with 15 decimal positions
            double maxDouble = double.MaxValue;

            Console.WriteLine("Max Int : " + maxInt);
            Console.WriteLine("Max Long : " + maxLong);
            Console.WriteLine("Max Decimal : " + maxDecimal);
            Console.WriteLine("Max Float : " + maxFloat);
            Console.WriteLine("Max Double : " + maxDouble);

            var anotherName = "Tom";
            // anotherName = 2; Cannot implicitly convert a gype 'int' to a 'string' 

            Console.WriteLine("anotherName is a {0}", anotherName.GetTypeCode());

            // Math

            Console.WriteLine("5 + 3 = " + (5 + 3));
            Console.WriteLine("5 - 3 = " + (5 - 3));
            Console.WriteLine("5 * 3 = " + (5 * 3));
            Console.WriteLine("5 / 3 = " + (5 / 3));
            Console.WriteLine("5.2 % 3 = " + (5.2 % 3));

            int i = 0;

            Console.WriteLine("i++ = " + (i++));
            Console.WriteLine("++i = " + (++i));
            Console.WriteLine("i-- = " + (i--));
            Console.WriteLine("--i = " + (--i));

            Console.WriteLine("i +- 3 = " + (i + -3));
            Console.WriteLine("i -= 2 = " + (i -= 2));
            Console.WriteLine("i *= 2 = " + (i *= 2));
            Console.WriteLine("i /= 2 = " + (i /= 2));
            Console.WriteLine("i %= 2 = " + (i %= 2));

            // casting

            // if no magnitude is lost, casting will happen automatically. But otherwise, you set it up as follows

            double pi = 3.14;
            int intPi = (int)pi;
            Console.WriteLine("intPi = " + intPi);

            // Math functions
            // Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Sin, Sinh, Tan, Tanh

            double number1 = 10.5;
            double number2 = 15;

            Console.WriteLine("number1 is " + number1);
            Console.WriteLine("number2 is " + number2);

            Console.WriteLine("Math.Abs(number1) " + (Math.Abs(number1)));
            Console.WriteLine("Math.Ceiling(number2) " + (Math.Ceiling(number1)));
            Console.WriteLine("Math.Floor(number1) " + (Math.Floor(number1)));
            Console.WriteLine("Math.Max(number1,number2) " + (Math.Max(number1, number2)));
            Console.WriteLine("Math.Min(number1,number2) " + (Math.Min(number1, number2)));
            Console.WriteLine("Math.Pow(number1, 2) " + (Math.Pow(number1, 2)));
            Console.WriteLine("Math.Round(number1) " + (Math.Round(number1)));
            Console.WriteLine("Math.Sqrt(number1) " + (Math.Sqrt(number1)));

            // random numbers

            Random rand = new Random();
            Console.WriteLine("Random number between 1 and 10 is " + rand.Next(1, 11));

            // Relational operators : > < >= <= == !=
            // Logical operators : && || ^ !  
            // note: ^ is the exclusive or

            Console.WriteLine("What is your child's age? (enter 0 - 18)");
            int age = int.Parse(Console.ReadLine());

            if ((age >= 5) && (age <= 7))
            {
                Console.WriteLine("Go to Elementary School");
            }
            else if ((age > 7) && (age <= 13))
            {
                Console.WriteLine("Go to middle school");
            }
            else if ((age < 4) || (age > 18))
            {
                Console.WriteLine("Your child does not meet our age requirements.");
            }
            else
            {
                Console.WriteLine("Go to high school");
            }


            Console.WriteLine("What is your age? ");
            int workingAge = int.Parse(Console.ReadLine());

            if ((workingAge < 14) || (workingAge > 67))
            {
                Console.WriteLine("You shouldn't work.");
            }
            else
            {
                Console.WriteLine("Work harder and smarter to get ahead.");
            }
        }
    }
}

Please help.

3 个答案:

答案 0 :(得分:5)

.Read() reads only a single character. You probably want .ReadLine() instead, which reads all the characters up to Enter, and returns a string.

int workingAge = int.Parse(Console.ReadLine());

答案 1 :(得分:2)

Expanding off of @recursive you can use int32.TryParse() to check if a valid number was supplied.

bool valid = false;
int workingAge;
while (!valid)
{
    valid = int32.TryParse(Console.ReadLine(), out workingAge);
    if (!valid)
        Console.WriteLine("Supplied number was invalid");
}
// Rest of code

Edit: I think this can be simplified even further by doing the following:

int workingAge;
while (!int32.TryParse(Console.ReadLine(), out workingAge))
{
    Console.WriteLine("Supplied number was invalid");
}
// Rest of code

答案 2 :(得分:0)

I will add to recursive's solution and explain what is happening.

since you are using Console.Read() in two different areas - and since Console.Read() reads one key input (i.e '1') - you are probably entering two different digits in the first occurance which causes the first .Read() to only read the first digit, and "passes" the second digit to the second .Read()

as was said before me, changing the .Read() to .ReadLine() will solve your issue.