帮助,我不知道这个错误是什么
Console.WriteLine (num1 + " + " + num2 " = " answer);
这会返回四个错误(即使用monodevelop)
这是整个代码
using System;
namespace CMD_test
{
class MainClass
{
public static void Main (string[] args)
{
Start:
double num1;
double num2;
double answer;
Console.WriteLine ("What operation shall we use? Type:");
Console.WriteLine ("A - for addition");
Console.WriteLine ("S - for subtraction");
Console.WriteLine ("M - for multiplication");
Console.WriteLine ("D - for division");
KeyPress:
string key = Console.Read ();
if (key == "A") {
Console.WriteLine ("You chose addition");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 + num2;
Console.WriteLine (num1 + " + " + num2 " = " answer);
}
if (key == "S") {
Console.WriteLine ("You chose subtraction");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 - num2;
Console.WriteLine (num1 + " - " + num2 " = " answer);
}
if (key == "M") {
Console.WriteLine ("You chose multiplication");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 * num2;
Console.WriteLine (num1 + " * " + num2 " = " answer);
} else if (key == "D") {
Console.WriteLine ("You chose division");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 / num2;
Console.WriteLine (num1 + " / " + num2 " = " answer);
} else {
Console.WriteLine ("You pressed a wrong button! Please retry.");
goto KeyPress;
}
Console.Write ("Do you want to do another operation? Y/N: ");
string restart = Console.ReadLine ();
if (restart == "Y") {
Console.Clear ();
goto Start;
} else if (restart == "N") {
Console.WriteLine ("Press any key to exit");
Console.ReadKey ();
}
}
}
}
也许还有一些我不知道的其他错误
答案 0 :(得分:6)
你的表达
Console.WriteLine (num1 + " + " + num2 " = " answer);
不是有效的,因为你已经错过了几个+
的表达。将其更改为:
Console.WriteLine (num1 + " + " + num2 + " = " + answer);
并且它正确编译。更好的是,这样做,你就可以避免这样的拼写错误:
Console.WriteLine ("{0} + {1} = {2}", num1, num2, answer);
哦,除此之外,当你切换到C#6时,你将能够做到这一点,这简化了事情:
Console.WriteLine ("{num1} + {num2} = {answer}");
答案 1 :(得分:3)
Console.WriteLine (num1 + " + " + num2 " = " answer);
实际应该是
Console.WriteLine (num1 + " + " + num2 + " = " + answer);
你错过了两个加号。这就是它产生错误的原因 您的IDE已告诉您问题的实际位置。
此外,以这种方式连接字符串不是一个好习惯。 Console.WriteLine
允许您使用字符串格式:
Console.WriteLine ("{0} + {1} = {2}", num1, num2, answer);
在这里阅读复合格式:
https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx
答案 2 :(得分:0)
以下是您尝试做的一个示例。