计算机科学- c# SimpleCalculator

本文关键字:SimpleCalculator 计算机科学 | 更新日期: 2023-09-27 17:50:59

我正试图为SIMPLE Calculator编写代码,我得到一些错误:

下面是错误:

错误1名称"Valid"在当前上下文中不存在
错误2名称"减"/"加"/"乘"、"除"在当前上下文中不存在

请看看什么是错误的代码和建议我做错了。

 int input = 0;
 double num1 = 0;
 double num2 = 0;
 string inputN1 = "";
 string inputN2 = "";
 bool validnum1 = true;
 bool validnum2 = true;
 do{
     Console.WriteLine("Simple Calculator'n't 1) Add'n't 2) Subtract'n't 3) 
     Multiply'n't 4) Divide'n't 5) Quit'n't ", input);
     Console.Write("Enter your Selection: ");
     input = Convert.ToInt32(Console.ReadLine());
     if (input ==5)
     {
        Console.WriteLine();
     }
     else if (input >5)
     {
        Console.WriteLine("Invalid Selection.'t Please Try Again");
     }
     else
     {
        Console.Write("Enter Number 1: ");
        inputN1 = Console.ReadLine();
        validnum1 = Valid(inputN1);
        Console.Write("Enter Number 2: ");
        validnum2 = Valid(inputN2);
        if (validnum1 == true && validnum2 == true)
        {
              num1 = Convert.ToDouble(inputN1);
              num2 = Convert.ToDouble(inputN2);
        }
        else 
        {
             Console.WriteLine("Invalid Number Entered");
             Console.ReadKey();
             break;
         }
         switch (input)
         {
            case 1:
                   Console.WriteLine("'tResults: {0}", Add(num1, num2));
                   break;
            case 2:
                   Console.WriteLine("'tResults: {0}", Subtract(num1, num2));
                   break;
            case 3:
                   Console.WriteLine("'tResults: {0}", Multiply(num1, num2));
                   break;
            case 4:
                   if (num2 == 0)
                   {
                      Console.WriteLine("Cant Divide by Zero/'t Please try Again");
                   }
                   else
                   {
                      Console.WriteLine("'tResults: {0}", Divide(num1, num2));
                   }
                   break;
           }
           Console.WriteLine("Press any key...");
           Console.ReadKey();
           Console.Clear();
       }
   }while (input != 5 && input <5);
      Console.WriteLine("Press any key....");
      Console.ReadKey();
 }
            }
        }

计算机科学- c# SimpleCalculator

首先,c#中没有有效、减法、加法和乘法这样的东西。检查给定的字符串是否有效double使用double.TryParse()。对于算术运算,分别使用+,-,*,/进行加,减,乘和除。相反,尝试使用这个代码,它将工作。如果您正在使用方法,那么通过类说明符访问它们,如Your_Class.Add(),因为要从静态类访问非静态方法,您需要包含非静态方法的类实例。

int input = 0;
        double num1 = 0;
        double num2 = 0;
        string inputN1 = "";
        string inputN2 = "";
        bool validnum1 = true;
        bool validnum2 = true;
        do{
            Console.WriteLine("Simple Calculator'n't 1) Add'n't 2) Subtract'n't 3) Multiply'n't 4) Divide'n't 5) Quit'n't ", input);
            Console.Write("Enter your Selection: ");
            input = Convert.ToInt32(Console.ReadLine());
            if (input ==5)
            {
                Console.WriteLine();
            }
            else if (input >5)
            {
                Console.WriteLine("Invalid Selection.'t Please Try Again");
            }
            else
            {
                Console.Write("Enter Number 1: ");
                inputN1 = Console.ReadLine();
                validnum1 = double.TryParse(inputN1,out num1);
                Console.Write("Enter Number 2: ");
                inputN2 = Console.ReadLine();
                validnum2 = double.TryParse(inputN2,out num2);
                if (validnum1 != true || validnum2 != true)
                {
                    Console.WriteLine("Invalid Number Entered");
                    Console.ReadKey();
                    break;
                   // num1 = Convert.ToDouble(inputN1);
                   // num2 = Convert.ToDouble(inputN2);
                }
                //else  else will not be required as double.tryparse is succeeded then value will automatically assigned to respective numbers.
                //{
                //}
                switch (input)
                {
                    case 1:
                        Console.WriteLine("'tResults: {0}", num1+num2);
                        break;
                    case 2:
                        Console.WriteLine("'tResults: {0}", num1-num2);
                        break;
                    case 3:
                        Console.WriteLine("'tResults: {0}", num1*num2);
                        break;
                    case 4:
                        if (num2 == 0)
                        {
                            Console.WriteLine("Cant Divide by Zero/'t Please try Again");
                        }
                        else
                        {
                            Console.WriteLine("'tResults: {0}", num1/num2);
                        }
                        break;
                }
                Console.WriteLine("Press any key...");
                Console.ReadKey();
                Console.Clear();
            }

        }while (input != 5 && input <5);
        Console.WriteLine("Press any key....");
        Console.ReadKey();
    }
    }
}
相关文章:
  • 没有找到相关文章