我是C#初学者,我在这里写了一个基本的循环计算器(下面的代码)。当我'退出'程序定义为循环中断时,我在第31行得到以下错误:“System.FormatException已被抛出未知的char”。有人可以解释这意味着什么以及如何编辑我的代码以避免这样的错误?谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
// Greeting.
Console.WriteLine ("Welcome to calculator.");
Console.WriteLine ("Enter 'Exit' to stop.");
// Persistent loop with an exit option.
while (true)
{
// Get three input values from user, separated by spaces.
Console.Write ("Input two values with an operation, with spaces between.");
// Get values from user.
string text = Console.ReadLine ();
string[] parts = text.Split (' ');
float value1 = Convert.ToSingle (parts [0]);
string operation = parts [1];
float value2 = Convert.ToSingle (parts [2]);
// Loop exit option.
if(text == "exit" || text == "Exit")
break;
// Establish the variable to store the result,
// initialized to zero, to be updated by the computation.
float result = 0;
// Switch between operations depending on the symbol input.
switch (operation)
{
case "+":
result = value1 + value2;
break;
case "-":
result = value1 - value2;
break;
case "*":
result = value1 * value2;
break;
case "/":
result = value1 / value2;
break;
case "%":
result = value1 % value2;
break;
case "^":
result = (float)Math.Pow (value1, value2);
break;
default:
Console.WriteLine ("ERROR");
break;
}
// Print out the result.
Console.WriteLine (value1 + " " + operation + " " + value2 + " = " + result);
}
}
}
}
答案 0 :(得分:1)
...
Console.WriteLine ("Welcome to calculator.");
Console.WriteLine ("Enter 'Exit' to stop.");
while(true) {
Console.Write ("Input two values with an operation, with spaces between.");
string text = Console.ReadLine ();
if(text == "exit" || text == "Exit")
break;
string[] parts = text.Split (' ');
...