如何在计算器中打印零? [英] How do I print zero in calculator?

查看:216
本文介绍了如何在计算器中打印零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是初学者,我需要创建一个计算器。问题是我知道如何使它如果4/0 = 0; 9/0 = 0等等。



<$ p $使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

名称空间Sav4
{
class程序
{
static void Main(string [] args)
{

char x;
加倍;
双b;
双倍;
Console.WriteLine(插入第一个数字);
a = double.Parse(Console.ReadLine());
Console.WriteLine(插入第二个数字);
b = double.Parse(Console.ReadLine());
Console.WriteLine(insert inc); // - + * /
x =(char)Console.Read();
Console.Clear();
if(x ==' - ')
{
ats = a - b;
Console.WriteLine({0} - {1} = {2},a,b,ats);
}
else
{
if(x =='+')
{

ats = a + b;
Console.WriteLine({0} + {1} = {2},a,b,ats);
}
else
{
if(x =='*')
{
ats = a * b;
Console.WriteLine({0} * {1} = {2},a,b,ats);
}
else
{
if(x =='/')
{
ats = a / b;
if(a / b!= 0)
{
Console.WriteLine({0} / {1} = {2},a,b,ats);
}
其他
{
Console.WriteLine(Lygybe neegzistuoja,a,b,ats);
}
}
其他
{
Console.WriteLine(错误);
}
}}
}
}
}

}





我尝试过的事情:



我想这很多就是

解决方案

如果 b 为零,则尝试执行此操作:

 ats = a / b; 

会给你一个例外,因为除以零不会给你一个真实世界的数字,你可以存储在 double

非常肯定,0不是正确的结果!



所以有两种方法可以处理它:

1)在你分手之前检查!在尝试之前进行快速测试,并报告问题,如果它不起作用而不是这样做。

2)使用尝试... catch 阻止检测错误并优雅地处理错误: try-catch (C#参考)| Microsoft Docs [ ^ ]


你好,



以下是我们划分任何内容时打印零的代码加倍。

假设我们有:

 double a = 10,b = 0,c = 0; 
尝试
{
c = double.IsInfinity(a / b)? 0:a / b;
}
最后
{
Console.WriteLine(c);
}





Quote:

如果我们使用double,那么我们没有得到异常DivideByZeroException,那么它只返回无穷大。 Double还有一种检查无穷大的方法,即double.IsInfinity()。使用此方法,您可以检查结果是否无穷大,您可以采取适当的措施。



在您的情况下,代码将是:



 arts = double.IsInfinity(a / b)? 0:a / b; 





谢谢。


So im a beginner and i needed to create a calculator. The problem is i downt know how to make it so that if "4 / 0 = 0; 9/0= 0" and so on.

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

namespace Sav4
{
    class Program
    {
        static void Main(string[] args)
        {
            
            char x;
            double a;
            double b;
            double ats;
            Console.WriteLine("insert first number ");
            a = double.Parse(Console.ReadLine());
            Console.WriteLine("insert second number ");
            b = double.Parse(Console.ReadLine());
            Console.WriteLine("insert inc "); // - + * /
            x = (char)Console.Read();
            Console.Clear();
            if (x == '-' )
            {
                ats = a - b;
                Console.WriteLine("{0} - {1} = {2}", a, b, ats);
            }
            else
            {
                if (x == '+')
                {

                    ats = a + b;
                    Console.WriteLine("{0} + {1} = {2}", a, b, ats);
                }
                else
                {
                    if (x == '*')
                    {
                        ats = a * b;
                        Console.WriteLine("{0} * {1} = {2}", a, b, ats);
                    }
                    else
                    {
                        if (x == '/')
                        {
                            ats = a / b;
                            if (a/b != 0)
                            {
                                Console.WriteLine("{0} / {1} = {2}", a, b, ats);
                            }
                            else
                            {
                                Console.WriteLine("Lygybe neegzistuoja", a, b, ats);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error");
                        }
                        } }
                }
            }
            }

        }



What I have tried:

I guess thinking a lot thats it

解决方案

If b is zero, then trying to do this:

ats = a / b;

will give you an exception because division by zero does not give you a "real world" number you can store in a double
Very definately, 0 is not the correct result!

So there are two ways to handle it:
1) Check before you divide! Put a quick test in before you try, and report a problem if it won't work instead of doing it.
2) Use a try...catch block to detect errors and handle them gracefully: try-catch (C# Reference) | Microsoft Docs[^]


Hi there,

Following is the code to print zero if we are dividing any double by zero.
Suppose we have:

double a = 10, b = 0, c=0;
          try
          {
              c = double.IsInfinity(a / b) ? 0 : a / b;
          }
          finally
          {
              Console.WriteLine(c);
          }



Quote:

Here if we are using double, then we are not getting the exception "DivideByZeroException" then it just returns infinity. Double has also a method to check the infinity i.e. "double.IsInfinity()". Using this method you can check whether the result is infinity or not and you can take appropriate action.


In your case, the code will be :

arts=double.IsInfinity(a / b) ? 0 : a / b;



Thanks.


这篇关于如何在计算器中打印零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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