Read()只接受第一个输入,不接受来自其他Console.Read()的其他输入

本文关键字:其他 Read 输入 Console 第一个 不接受 | 更新日期: 2023-09-27 18:05:05

我遵循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社区版上运行了这个,它正在做同样的事情,好像这些变化没有影响:

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()只接受第一个输入,不接受来自其他Console.Read()的其他输入

.Read()只读取一个字符。您可能需要.ReadLine(),它读取所有字符直到输入,并返回string

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

展开@recursive,你可以使用int32.TryParse()来检查是否提供了一个有效的数字

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

编辑:我认为这可以通过以下操作进一步简化:

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

我将添加递归的解决方案并解释发生了什么。

,因为您在两个不同的区域使用Console.Read() -并且由于Console.Read()读取一个键输入(即'1')-您可能在第一次出现时输入了两个不同的数字,这导致第一个.Read()只读取第一个数字,并将第二个数字"传递"给第二个.Read()

如我之前所说,将.Read()更改为.ReadLine()将解决您的问题。