如何解决错误:无法将int隐式转换为字符串

时间:2018-07-28 22:35:09

标签: c#

我只需要一些帮助。我正在做一个基本的计算器。

我希望我的计算器在用户尝试输入字符串或十进制值时出现错误消息。但是当我完成编码后,由于出现错误

,我无法声明我的int变量(firstNum2secondNum2thirdNum2)。
  

无法将int隐式转换为字符串

代码

Console.WriteLine("Enter A First Num");
firstNum = Console.ReadLine();
char[] firstNumArray = firstNum.ToCharArray();
int length = firstNum.Length;

for (int a = 0; a < length; a++)
{
   if (!(firstNum[a]
            .Equals('0') || firstNum[a]
            .Equals('1') || firstNum[a]
            .Equals('2') || firstNum[a]
            .Equals('3') || firstNum[a]
            .Equals('4') || firstNum[a]
            .Equals('5') || firstNum[a]
            .Equals('6') || firstNum[a]
            .Equals('7') || firstNum[a]
            .Equals('9')))

   {


      if (firstNumArray[a]
         .Equals('.'))
      {
         Console.WriteLine("You just inputted a decimal value. Enter another value");
         goto Start1;
      }
      else if (firstNum == "EXIT")
      {
         Console.WriteLine("Kdot");
         Console.ReadKey();
      }
      else if (int.TryParse(firstNum, out firstNum1))
      {

         Console.WriteLine("Your number is " + firstNum1);
      }
      else
      {
         Console.WriteLine("You just inputted a string/ char value. Try again.");

         goto Start1;
      }

   }
}

Start2:

Console.WriteLine("Enter A Second Num");
secondNum = Console.ReadLine();
char[] secondNumArray = secondNum.ToCharArray();
int length1 = secondNum.Length;

for (int b = 0; b < length1; b++)
{
   if (!(secondNum[b]
            .Equals('0') || secondNum[b]
            .Equals('1') || secondNum[b]
            .Equals('2') || secondNum[b]
            .Equals('3') || secondNum[b]
            .Equals('4') || secondNum[b]
            .Equals('5') || secondNum[b]
            .Equals('6') || secondNum[b]
            .Equals('7') || secondNum[b]
            .Equals('9')))
   {

      if (secondNumArray[b]
         .Equals('.'))
      {
         Console.WriteLine("You just inputted a decimal value. Enter another value");
         goto Start2;
      }
      else if (secondNum == "EXIT")
      {
         Console.WriteLine("Kdot");
         Console.ReadKey();
      }
      else if (int.TryParse(secondNum, out secondNum1))
      {

         Console.WriteLine("Your number is " + secondNum1);
         secondNum2 = Convert.ToInt32(secondNum1);

      }
      else
      {
         Console.WriteLine("You just inputted a string/ char value. Try again.");

         goto Start2;
      }

   }
}

Start3:
Console.WriteLine("Enter A Third Num");
thirdNum = Console.ReadLine();
char[] thirdNumArray = thirdNum.ToCharArray();
int length2 = thirdNum.Length;

for (int c = 0; c < length1; c++)
{
   if (!(thirdNum[c]
            .Equals('0') || thirdNum[c]
            .Equals('1') || thirdNum[c]
            .Equals('2') || thirdNum[c]
            .Equals('3') || thirdNum[c]
            .Equals('4') || thirdNum[c]
            .Equals('5') || thirdNum[c]
            .Equals('6') || thirdNum[c]
            .Equals('7') || thirdNum[c]
            .Equals('9')))
   {

      if (thirdNumArray[c]
         .Equals('.'))
      {
         Console.WriteLine("You just inputted a decimal value. Enter another value");
         goto Start3;
      }
      else if (thirdNum == "EXIT")
      {
         Console.WriteLine("Kdot");
         Console.ReadKey();
      }
      else if (int.TryParse(thirdNum, out thirdNum1))
      {

         Console.WriteLine("Your number is " + thirdNum1);
         thirdNum2 = Convert.ToInt32(thirdNum1);
      }
      else
      {
         Console.WriteLine("You just inputted a string/ char value. Try again.");

         goto Start3;
      }

   }
}

//here the error occurs

sum = firstNum2 + secondNum2 + thirdNum2;
diff = firstNum2 - secondNum2 - thirdNum2;
prod = firstNum2 * secondNum2 * thirdNum2;
quo = firstNum2 / secondNum2 / thirdNum2;
mod = firstNum2 % secondNum2 % thirdNum2;


Console.WriteLine(sum);
Console.WriteLine(diff);
Console.WriteLine(prod);
Console.WriteLine(quo);
Console.WriteLine(mod);

1 个答案:

答案 0 :(得分:2)

您对此有很多问题,本质上(而且我不知道在哪里),您的结果之一实际上是字符串而不是int。即sumdiff等...

尽管如此,但还有许多其他细微的问题

首先需要查找Don't repeat yourself (DRY)

  

在软件工程中,不要重复自己(DRY)的原则   旨在减少软件重复的软件开发   模式,用抽象代替,或重复相同   数据,使用数据规范化来避免冗余。

第二件事是使用goto语句非常不寻常。而是使用while循环,在这种情况下,我只是做了一个无限循环

让我们提供一种处理输入的方法

// returning false signifies exit 
public static bool EnterNumber(out int result)
{
   result = 0;

   // read from the console
   var line = Console.ReadLine();
   while (true)
   {
      // if its exit return false
      if (line == "EXIT")
         return false;

      // its an int yay!!!! return true
      if (int.TryParse(line, out result))
         return true;

      // its a decimal, underscore means just discard the value
      if (decimal.TryParse(line, out _))
         Console.WriteLine("You just inputted a decimal value. Enter another value");
      else
         Console.WriteLine("Invalid Input. Try again.");
      line = Console.ReadLine();
   }  
}

然后,您只需使用它并检查结果

static void Main(string[] args)
{
   Console.WriteLine("Enter first number");

   // this is not quite DRY, however it is easy to read
   if (!EnterNumber(out var firstNum))
   {
      Console.WriteLine("Kdot");
      Console.ReadKey();
      return;
   }

   Console.WriteLine("Enter second number");
   if (!EnterNumber(out var secondNum))
   {
      Console.WriteLine("Kdot");
      Console.ReadKey();
      return;
   }

   Console.WriteLine("Enter third number");
   if (!EnterNumber(out var thirddNum))
   {
      Console.WriteLine("Kdot");
      Console.ReadKey();
      return;
   }

   Console.WriteLine($"Addition = {firstNum + secondNum + thirddNum}");
   Console.WriteLine($"Subtraction = {firstNum - secondNum - thirddNum}");
   Console.WriteLine($"Multiplication = {firstNum * secondNum * thirddNum}");
   // you need to cast int to double otherwise you will get unexpected results
   Console.WriteLine($"Division = {(double)firstNum /(double) secondNum / (double)thirddNum}");
   Console.WriteLine($"Modulo = {firstNum % secondNum % thirddNum}");
   Console.ReadKey();
}

输出

Enter first number
test
Invalid Input. Try again.
4.4
You just inputted a decimal value. Enter another value
1
Enter second number
2
Enter third number
3
Addition = 6
Subtraction = -4
Multiplication = 6
Division = 0.166666666666667
Modulo = 1