不接受小数作为输入的计算器 [英] Calculator that does not accept decimals as input
本文介绍了不接受小数作为输入的计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Accessible_Calculater
{
class Program
{
private static object e;
private static int num1;
public static string Keypressed { get; private set; }
public static object Keys { get; private set; }
public static object Answer { get; private set; }
static void Main(string[] args)
{
while (true)
{
int num1;
int num2;
string operand;
float answer;
Console.Write("Please enter the first integer: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter an operand (+, -, /, *): ");
operand = Console.ReadLine();
Console.Write("Please enter the second integer: ");
num2 = Convert.ToInt32(Console.ReadLine());
switch (operand)
{
case "-":
answer = num1 - num2;
break;
case "+":
answer = num1 + num2;
break;
case "/":
answer = num1 / num2;
break;
case "*":
answer = num1 * num2;
break;
default:
answer = 0;
break;
}
Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString());
Console.ReadLine();
}
}
}
}
推荐答案
你好
我建议您将输入的String转换为Decimal类型的对象.
I suggest you convert the input String to Decimal type object.
由于Int类型无法处理十进制值.另外,请注意,
As Int types can not handle decimal values. Also, as a note,
您正在使用无限的While(true)循环.重新编码出口
you are using While(true) loop that is infinite. ReCode an exit
打破循环.例如,如果String == Null,则Break
to break the loop. Such as, If String == Null then Break
希望有帮助. :)
这篇关于不接受小数作为输入的计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文