为什么键入是后控制台关闭? [英] Why does the console close after I type yes?

查看:71
本文介绍了为什么键入是后控制台关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中制作计算器。这是乘法部分---



 使用系统; 
static class calculator
{
public static void Main()
{
welcome:
Console.WriteLine( 欢迎使用我的计算器,请按enter继续);
Console.ReadLine();
Console.WriteLine( 你想加,减,乘或除吗? );
string x = Convert.ToString(Console.ReadLine());
if (x == multiply
{
Console.WriteLine( 请输入第一个值< /跨度>);
decimal value1multiply = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine( 请输入第二个值);
decimal value2multiply = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine( Result =);
Console.WriteLine(value1multiply * value2multiply);
Console.WriteLine( 感谢您使用我的计算器!您还想使用它吗?请回答在'是'或'否'并按'输入');
Console.ReadLine();
string yesorno = Console.ReadLine();
if (yesorno == yes
{
goto welcome;
}
如果(yesorno == no
{
Environment.Exit( 0 );
}
}
}
}









当我要求我输入是时,控制台应引导我欢迎。但相反,它并没有引导我任何地方,仍然是空白。当我再次按回车键时,控制台关闭。为什么会发生这种情况?如何预防?



我尝试过的事情:



我尝试删除environment.exit(0),认为控制台正在引导我,但它没有帮助。我甚至尝试在Visual Studio中键入代码,但结果没有区别。 (我使用sharp develop)

解决方案

你有两个readline语句



 Console.WriteLine( 感谢您使用我的计算器!您还想使用它吗?请回答'是'或'否'并按'输入'); 
Console.ReadLine();
string yesorno = Console.ReadLine();





因此,当您输入是并按下由第一个吸收的返回时,所以下一行读入yesorno。只需删除第一条readline



 Console.WriteLine( 感谢您使用我的计算器!您还想使用它吗?请回答是或否并按输入); 
// Console.ReadLine();
string yesorno = Console.ReadLine();





至于你的解决方案,一般来说,转到声明已经使用了大约15年,这是非常糟糕的做法。而是尝试这样的事情



 静态  void  Main( string  [] args)
{
bool running = true ;
while (正在运行)
{
Console.WriteLine( 欢迎使用我的计算器,请按enter继续);
Console.ReadLine();
Console.WriteLine( 你想加,减,乘或除吗? );
string x = Convert.ToString(Console.ReadLine());
if (x == multiply
{
Console.WriteLine( 请输入第一个值< /跨度>);
decimal value1multiply = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine( 请输入第二个值);
decimal value2multiply = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine( Result =);
Console.WriteLine(value1multiply * value2multiply);
Console.WriteLine( 感谢您使用我的计算器!您还想使用它吗?请回答在'是'或'否'并按'输入');
string yesorno = Console.ReadLine();
if (yesorno == no
{
running = false ;
}
}
}
}


 Console.ReadLine( );  //   ***不需要 
string yesorno = Console.ReadLine();



您需要额外调用 Console.ReadLine ,这会消耗是或否响应。删除该行,它应该工作。此外,不建议使用 goto ,您应该使用某种循环结构。


如果您已将代码放入Visual Studio随后阅读本文掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



如果您单步执行代码,您将能够看到正在发生的事情,并希望能够找出原因。


如果您更喜欢使用SharpDevelop进行调试,那么本文应该有所帮助 - IT管理:使用C#|深入平台入门| InformIT [ ^

I was making a calculator in C#. Here is the multiplication part---

using System;
static class calculator 
{
	public static void Main() 
	{
	welcome:
		Console.WriteLine("Welcome to my calculator, please press enter to continue");
		Console.ReadLine();
		Console.WriteLine("Do you want to add, subtract, multiply or divide?");
		string x = Convert.ToString(Console.ReadLine());
		if (x == "multiply") 
		{
			Console.WriteLine("Please enter the first value");
			decimal value1multiply = Convert.ToDecimal( Console.ReadLine());
			Console.WriteLine("Please enter the second value" );
			decimal	value2multiply = Convert.ToDecimal(Console.ReadLine());
			Console.WriteLine("Result =");
			Console.WriteLine(value1multiply * value2multiply);
			Console.WriteLine("Thank you for using my calculator!Do you still want to use it?Please answer in 'yes' or 'no' and press 'enter'");
			Console.ReadLine();
			string yesorno =Console.ReadLine();
			if (yesorno == "yes") 
			{
				goto welcome;
			}
			 if (yesorno == "no") 
			 {
				Environment.Exit(0);
			}
		}
             }
           }





When I type 'yes' when it asks me to, the console should lead me to welcome. But instead, it does not lead me anywhere and remains blank. When I press enter again, the console closes. Why is this happening and how can I prevent it?

What I have tried:

I tried removing the environment.exit(0), thinking that the console was leading me to that but it did not help. I even tried typing the code in Visual Studio, but no difference in the outcome. (I use sharp develop)

解决方案

You have two readline statements

Console.WriteLine("Thank you for using my calculator!Do you still want to use it?Please answer in 'yes' or 'no' and press 'enter'");
Console.ReadLine();
string yesorno = Console.ReadLine();



So when you enter "yes" and press return that is absorbed by the first one, so it is the next line read into yesorno. Just remove the first readline

Console.WriteLine("Thank you for using my calculator!Do you still want to use it?Please answer in 'yes' or 'no' and press 'enter'");
//Console.ReadLine();
string yesorno = Console.ReadLine();



As for your solution in general, the "goto" statement hasn't been used for about 15 years, it is very bad practice. Instead try something like this

static void Main(string[] args)
{
    bool running = true;
    while (running)
    {
        Console.WriteLine("Welcome to my calculator, please press enter to continue");
        Console.ReadLine();
        Console.WriteLine("Do you want to add, subtract, multiply or divide?");
        string x = Convert.ToString(Console.ReadLine());
        if (x == "multiply")
        {
            Console.WriteLine("Please enter the first value");
            decimal value1multiply = Convert.ToDecimal(Console.ReadLine());
            Console.WriteLine("Please enter the second value");
            decimal value2multiply = Convert.ToDecimal(Console.ReadLine());
            Console.WriteLine("Result =");
            Console.WriteLine(value1multiply * value2multiply);
            Console.WriteLine("Thank you for using my calculator!Do you still want to use it?Please answer in 'yes' or 'no' and press 'enter'");
            string yesorno = Console.ReadLine();
            if (yesorno == "no")
            {
                running = false;
            }
        }
    }
}


Console.ReadLine();  // *** not needed
string yesorno =Console.ReadLine();


You have an extra call to Console.ReadLine which consumes the yes or no response. Remove that line and it should work. Also the use of goto is not recommended, you should use some sort of loop construct.


If you have put the code into Visual Studio then read this article Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

If you step through your code you will be able to see what is happening and hopefully be able to work out why.

If you prefer to debug using SharpDevelop then this article should help - IT Management: Dipping into the Platform with C# | Getting Started | InformIT[^]


这篇关于为什么键入是后控制台关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆