我遵循的C#教程视频中的说明很棒。我正在使用视频中的注释和代码构建文件。

我查看了类似的问题,但它们不能解决此问题。

这是CS文件的副本:

        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.");
            }

       }


该程序将忽略以下输入:

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


输出为:

What is your age?
You shouldn't work.


因此,该程序并没有停止我的输入,而是似乎根据先前的整数输入值2或5处理其条件。

其他文章谈到了做下面的尝试,但都无济于事:

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




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


第二个在Visual Studio The name 'int32' does not exist in the current context中产生了错误

我更新了脚本以使用int.Parse(Console.ReadLine()),并且该脚本可在Visual Studio 2007上使用,但我在Visual Studio 2015 Community Edition上运行了该脚本,它所做的完全相同,就好像更改没有效果:

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.");
            }
        }
    }
}


请帮忙。

最佳答案

.Read() reads only a single character。您可能需要.ReadLine()代替,它读取所有字符直到Enter,然后返回string

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

关于c# - Read()仅接受第一个输入,而没有其他控制台的其他输入。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39255901/

10-10 18:30
查看更多