我的目标是创建一个简单的代码,它接受输入并给出数字的平方。我使用另一种方法进行计算,但我认为方法设置有误。请看一下。
using System;
namespace NumberM {
class InputLocation {
public static void Main (string [] args) {
int PlsCall;
begin:
Console.Write ("Please specify a number : ");
string NumInput = Console.ReadLine ();
int NumNewValue = Int32.Parse (NumInput);
Console.WriteLine("Is {0} the number you specified?", NumNewValue);
string Ans = Console.ReadLine();
if (Ans == "yes" || Ans == "Yes") {
Console.WriteLine ("That means {0} is your number. All right. Calculating.");
// disable once SuggestUseVarKeywordEvident
CalcNumb InP = new CalcNumb();
PlsCall = InP.Calculation (NumNewValue);
} else {
Console.WriteLine ("That might be an issue. Taking back.");
goto begin;
}
}
}
class CalcNumb {
public int Calculation (int number) {
int Store = number * number;
Console.WriteLine ("This is the square of your number : {0}" , Store);
}
}
}
我在调用方法的第35和26行收到错误。 PlsCall = InP.Calculation(NumNewValue); public int Calculation(int number){
我得到这样的东西。我不能很好地翻译,但如果你需要翻译,那就是土耳其文。
'NumberM.CalcNumb.Calculation(int)':tüm柯yollarıdeğeröndürmez(CS0161)
提前感谢您的帮助。
答案 0 :(得分:4)
计算方法是一个整数,你不会返回任何东西
public int Calculation (int number)
{
int Store = number * number;
Console.WriteLine ("This is the square of your number : {0}" , Store);
return Store; // Needed
}
答案 1 :(得分:3)
您的方法计算未返回值
答案 2 :(得分:2)
你必须退货!
public int Calculation (int number)
{
int Store = number * number;
Console.WriteLine ("This is the square of your number : {0}" , Store);
return Store;
}
此行中的另一个错误:
Console.WriteLine ("That means {0} is your number. All right. Calculating.");
应该是:
Console.WriteLine ("That means {0} is your number. All right. Calculating.", NumNewValue);
并且,请避免使用C#代码中的goto
;)
答案 3 :(得分:2)
替换
class CalcNumb {
public int Calculation (int number) {
int Store = number * number;
Console.WriteLine ("This is the square of your number : {0}" , Store);
}
}
到
class CalcNumb {
public int Calculation (int number) {
int Store = number * number;
Console.WriteLine ("This is the square of your number : {0}" , Store);
return Store;
}
}