c#console添加或减去计算器

时间:2016-09-12 12:07:00

标签: c#

我是c#beginner所以我的c#console计算器有问题这里是代码

Console.WriteLine("If you want to add click 1 , subtract click 2");
int userchoice = int.Parse(Console.ReadLine());

if (userchoice == 1)
{
    Console.WriteLine("Enter a number");
    int firstinput = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter another number");
    int secondinput = int.Parse(Console.ReadLine());
    Console.WriteLine("Click =");
    string choice = Console.ReadLine();
    if (choice == "=")
    {
        Console.WriteLine(firstinput + secondinput);
    }
    else  if (userchoice == 2)
    {
        Console.WriteLine("Enter a number");
        int firstinput1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter another number");
        int secondinput1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Click =");
        string choice2 = Console.ReadLine();
        if (choice2 == "=")
        {
            Console.WriteLine(firstinput1 - secondinput1);
        }

问题是,如果我输入1,它将进行加法操作,但如果我输入2则它不会做任何事情为什么虽然我写的代码是正确的答案。

2 个答案:

答案 0 :(得分:0)

你混合了else语句。现在您的else if (userchoice == 2)匹配语句if (choice == "=")。尝试:

if (userchoice == 1)
{
    Console.WriteLine("Enter a number");
    int firstinput = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter another number");
    int secondinput = int.Parse(Console.ReadLine());
    Console.WriteLine("Click =");
    string choice = Console.ReadLine();
    if (choice == "=")
    {
        Console.WriteLine(firstinput + secondinput);
    }
}
else if (userchoice == 2)
{
        Console.WriteLine("Enter a number");
        int firstinput1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter another number");
        int secondinput1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Click =");
        string choice2 = Console.ReadLine();
        if (choice2 == "=")
        {
            Console.WriteLine(firstinput1 - secondinput1);
        }
 }
else
{
   Console.WriteLine("Wrong Selection, either select 1 or 2");
}

答案 1 :(得分:0)

@FatmaHamdy,你在其他部分之前错过了}

Console.WriteLine("If you want to add click 1 , subtract click 2");
int userchoice = int.Parse(Console.ReadLine());

if (userchoice == 1)
{
    Console.WriteLine("Enter a number");
    int firstinput = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter another number");
    int secondinput = int.Parse(Console.ReadLine());
    Console.WriteLine("Click =");
    string choice = Console.ReadLine();
    if (choice == "=")
    {
        Console.WriteLine(firstinput + secondinput);
    }
}
    else  if (userchoice == 2)
    {
        Console.WriteLine("Enter a number");
        int firstinput1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter another number");
        int secondinput1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Click =");
        string choice2 = Console.ReadLine();
        if (choice2 == "=")
        {
            Console.WriteLine(firstinput1 - secondinput1);
        }
}