简体   繁体   English

为什么我的代码跳过我的控制台。在第一个之后读取?

[英]Why does my code skip my Console.Read after the first one?

Okay, so here is what I have, the console runs and accepts any number for the first input and outputs all the outputs but it skips the rest of the reads after the first one. 好的,这就是我所拥有的,控制台将运行并为第一个输入接受任何数字,并输出所有输出,但是它将跳过第一个之后的其余读取。 I am new to C# and want to learn so I definetly feel like I am just missing something stupid here. 我是C#的新手,想学习,所以我绝对觉得我在这里缺少一些愚蠢的东西。 Do not worry about the purpose as I only want help with this problem I am facing now. 不必担心目的,因为我仅想解决我现在面临的这个问题。

Thanks guys, 多谢你们,

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

namespace Stamps
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, and welcome to the U.S. Mail Stamp Printer. Now, to print the correct stamp for your postage, please enter the number of half ounces (so 1= .5 ounce, 2 = 1 ounce) your package will weigh \n");
            double i = Console.Read();

            Console.WriteLine("Thank you, now, will you be sending to zone 1, 2, or 3? Simply enter the zone number and press enter\n");
            double z = Console.Read();

            Console.WriteLine("Great! Now, last question before I print your stamp, will you be using Local Mail or Air Mail, use the number 1 for local and the number 2 for air.\n");
            double m = Console.Read();


            if (m == 2)
            {
                double m2 = .95;


            }
            else
            {
                double m2 = .49;
            }


            if( i == 1)
            {
               double i2 = 1;
            }



            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
            }
            }
            }

The Console.Read method doesn't parse input, it only reads one character from the input stream. Console.Read方法不解析输入,它仅从输入流中读取一个字符。

Use the Console.ReadLine method and parse the string that you get: 使用Console.ReadLine方法并解析获得的字符串:

double i = Double.Parse(Console.ReadLine());

or: 要么:

int z = Int32.Parse(Console.ReadLine());

If you want to handle invalid input you would use the TryParse methods, they will return a boolean that tell you if the parsing worked or not. 如果要处理无效输入,则可以使用TryParse方法,它们将返回一个布尔值,告诉您解析是否起作用。


Side note: When you try to use the m2 and i2 variables you will notice that it doesn't exist. 旁注:当您尝试使用m2i2变量时,您会注意到它不存在。 The variable is local for the code blocks in the if statement, so you need to declare it outside to be able to use it later: 该变量是if语句中代码块的局部变量,因此您需要在外部声明它以便以后使用:

double m2;
if (m == 2)
{
  m2 = .95;
}
else
{
  m2 = .49;
}

you can flush you console buffer or you can used console.readline(). 您可以刷新控制台缓冲区,也可以使用console.readline()。

For more information refer below post which give yours question answer. 有关更多信息,请参见下面的文章,该文章为您提供了问题的答案。

http://stackoverflow.com/questions/7302544/flushing-system-console-read

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM