C#WriteLine Works,写入失败 - 古怪的bug?

时间:2014-01-12 15:23:21

标签: c# string methods

所以第59行有我只能描述为wackadoodle错误(如果我正确理解我的代码),如果你离开带有Console.ReadLine()的返回行,那么文件就会运行,如果如果将其更改为Console.Read()),该文件在运行时将产生错误。奇怪的是,它不应该运行,因为我不调用函数或执行实际的console.writes等。所以我希望有人可以帮助我理解这一点,并确认我的思路正确我有一些古怪的代码,或者我对代码运行方式的理解是不正确的。

产生错误的代码:

public string GetStr(String StrVar)//note - using strings here
{ 
  Console.Write(StrVar);return Console.ReadLine().ToLower().Trim(); 
}

如果行return Console.ReadLine()更改为return Console.Read(),文件错误 - 但文件应该真正运行,因为我实际上没有调用任何东西 - 似乎字符串变量是某种方式自我如果我理解发生了什么,写信给控制台。

完整代码:

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

namespace a015_mealCalculator
{
    class Program
    {
        public void Play()
        {
            //DisplayChek("DisplayChek!");

            do //do while loop
            {
                DisplayStr(">>>-- Meal Calculator v1.3 --<<< \n\n\n");//Welcome

                //ask info
                String fName = GetStr("Enter your FIRST NAME here: ");
                String lName = GetStr("Enter your LAST NAME here: ");
                String rName = GetStr("Enter the NAME of the RESTURANT you are dinning at here: ");
                String wholeName = fName + " " + lName;
                double mealCost = GetDouble("How much was your meal " + fName + "?");
                String mealGreeting = "\n" + wholeName + ", your meal at " + rName + " was:";    
                //process math
                double tax = mealCost / 8;
                double tip = mealCost % 18;
                double totalCost = mealCost + tip + tax;

                tax = Math.Round(tax, 2);//trim decimals
                tip = Math.Round(tip, 2);//trim decimals
                totalCost = Math.Round(totalCost, 2);    

                //Announce results
                Console.WriteLine("\nMeal: " + mealCost);
                Console.WriteLine(mealGreeting);
                Console.WriteLine("Meal: $" + mealCost);
                Console.WriteLine("Tax: $" + tax);
                Console.WriteLine("Tip: $" + tip);
                Console.WriteLine("Total: $" + totalCost);

            } while (PlayAgain());

            DisplayStr("Enjoy Your Meal!"); //Salutation
        }

        //MaxBox
        public void DisplayChek(String StringNameIs)
        { Console.WriteLine("I am in " + StringNameIs); }//Where are we?

        public void DisplayStr(String StrTxt)
        { Console.WriteLine(StrTxt); }

        public void DisplayRs()
        { Console.Write("\n\n"); }

        public string GetStr(String StrVar)//note - using strings here
        { Console.Write(StrVar);return Console.ReadLine().ToLower().Trim(); }

        public double GetDouble(String doubleRequest)// We take in a STRING but we return a DOUBLE
        {
            Console.WriteLine(doubleRequest + ": "); // HERE we use the STRING to ask for the DOULBE
            return double.Parse(Console.ReadLine()); //HERE we RETURN the DOUBLE asked for!
        }

        public Boolean PlayAgain()
        {
            Console.Write("\n\nDo you want to play again? (y)es or (n)o: ");
            String command = Console.ReadLine().ToLower();

            if (command == "y" || command == "yes") return true;
            return false;
        }

        static void Main(string[] args)
        {
            Program MealCalculator = new Program();
            MealCalculator.Play();

            //Play keeps file open
            //Console.Read();
        }
    }
}

3 个答案:

答案 0 :(得分:3)

Console.Read()返回一个整数,ReadLine()返回一个字符串。

ReadLine方法,或KeyAvailable属性和ReadKey方法比使用Read方法更可取。

答案 1 :(得分:1)

这不会编译,因为Console.Read()返回一个int。你不能在int上执行.ToLower()。

答案 2 :(得分:1)

如果查看两种方法的文档:Console.Read()将从控制台流中读取的下一个字符的字符代码作为整数返回,而Console.ReadLine()则返回一行作为字符串。 double.Parse接受字符串参数,因此在第一种情况下存在类型不匹配。