2017-03-06 38 views
-1

在C# 简单的计算器,我无法理解为什么我得到采取任何的输入类计算器C#提供了一个错误,如果字符串转换成int

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     char choice = '7'; 
     while (choice != '3') 
     { 
      Console.WriteLine("---------------Calculator---------------"); 
      Console.WriteLine("\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Modulo-Operation (remainder)\n6.Exit\n"); 
      Console.WriteLine("Enter your choice:"); 
      choice = Convert.ToChar(Console.Read()); 
      switch (choice) 
      { 
       case '1': 
        Calculator.Add(); 
        break; 
       case '2': 
        Calculator.Subtract(); 
        break; 
       case '3': 
        Calculator.Multiply(); 
        break; 
       case '4': 
        Calculator.Divide(); 
        break; 
       case '5': 
        Calculator.Modulo(); 
        break; 
       case '6': 
        Environment.Exit(0); 
        break; 
       default: 
        Console.WriteLine("Invalid Choice.\n"); 
        break; 
      } 
     } 
    } 
} 

class Calculator 
{ 

    public static void Add() 
    { 
     int a, b; 
     string sa, sb; 
     Console.WriteLine("Enter the first number:\n"); 
     sa = Console.ReadLine().Trim(); 
     a = Convert.ToInt32(sa); 
     Console.WriteLine("Enter the second number:\n"); 
     sb = Console.ReadLine().Trim(); 
     b = Convert.ToInt32(sb); 
     Console.Write("{0}\n", a + b); 
    } 
    public static void Subtract() 
    { 
     int a, b; 
     string sa, sb; 
     Console.WriteLine("Enter the first number:\n"); 
     sa = Console.ReadLine().Trim(); 
     a = Convert.ToInt32(sa); 
     Console.WriteLine("Enter the second number:\n"); 
     sb = Console.ReadLine().Trim(); 
     b = Convert.ToInt32(sb); 
     Console.Write("{0}\n", a - b); 
    } 
    public static void Multiply() 
    { 
     int a, b; 
     string sa, sb; 
     Console.WriteLine("Enter the first number:\n"); 
     sa = Console.ReadLine().Trim(); 
     a = Convert.ToInt32(sa); 
     Console.WriteLine("Enter the second number:\n"); 
     sb = Console.ReadLine().Trim(); 
     b = Convert.ToInt32(sb); 
     Console.Write("{0}\n", a * b); 
    } 
    public static void Divide() 
    { 
     int a, b; 
     string sa,sb; 
     Console.WriteLine("Enter the first number:\n"); 
     sa = Console.ReadLine().Trim(); 
     a = Convert.ToInt32(sa); 
     Console.WriteLine("Enter the second number:\n"); 
     sb = Console.ReadLine().Trim(); 
     b = Convert.ToInt32(sb); 
     while (b == 0) 
     { 
      Console.WriteLine("In division operation, divisor cannot be zero.\n Retry!!\n"); 
      Console.WriteLine("Enter the second number:\n"); 
      sb = Console.ReadLine().Trim(); 
      b = Convert.ToInt32(sb); 
     } 
     Console.Write("{0}\n", a/b); 
    } 
    public static void Modulo() 
    { 
     int a, b; 
     string sa, sb; 
     Console.WriteLine("Enter the first number:\n"); 
     sa = Console.ReadLine().Trim(); 
     a = Convert.ToInt32(sa); 
     while (a < 0) 
     { 
      Console.WriteLine("In modulo operation, first number cannot be negative.\n Retry!!\n"); 
      Console.WriteLine("Enter the second number:\n"); 
      sb = Console.ReadLine().Trim(); 
      b = Convert.ToInt32(sb); 
     } 
     Console.WriteLine("Enter the second number:\n"); 
     sb = Console.ReadLine().Trim(); 
     b = Convert.ToInt32(sb); 
     Console.Write("{0}\n", a % b); 
    } 
} 
} 

例外,我总是得到一个错误,如“出现FormatException:”错误将字符串转换为DateTime

+0

请熬下来到[MCVE。另外,你想要转换为日期时间的具体字符串是什么? –

+0

如果您提供行号异常的行号,可以更快得到答案 – Fabio

+0

粘贴确切的异常。在这段代码中,你没有使用'DateTime',所以你不能将_error转换字符串转换为DateTime_ – Pikoh

回答

1

您的问题是在这条线

choice = Convert.ToChar(Console.Read()); 

这从输入读取只有一个字符,但如果按'1',然后按回车键,代码进入Add方法,然后立即检索您在1之后按下的换行符。
这不是有效整数,Convert.ToInt32失败。

更改为

string choice = Console.ReadLine().Trim(); 

和修改交换机来处理字符串,而不是一个char

说,你需要改变你的代码的另一个问题。如果您尝试转换非有效整数的输入,Convert.ToInt32将失败并出现异常。你应该看看如何使用Int32.TryParse并采取适当的行动,如果输入的是无效

例如

string sa, sb; 
Console.WriteLine("Enter the first number:\n"); 
sa = Console.ReadLine().Trim(); 
if(!Int32.TryParse(sa, out a)) 
{ 
    Console.WriteLine("Type a number"); 
    return; 
} 
+0

好抓。我仍然想知道为什么'Console.ReadLine()'没有等​​待输入。 :) – waka

+0

作为一种选择,因为OP只有一个1个字符来选择选项,所以你可以只做'choice = Convert.ToChar(Console.ReadKey()。KeyChar);'这样做的好处是不需要用户按回车 – Pikoh