gpt4 book ai didi

c# - 错误 CS1001(需要标识符)

转载 作者:太空狗 更新时间:2023-10-30 00:46:10 27 4
gpt4 key购买 nike

我是编程新手,正在学习 C# 类(class)。当我尝试编写此程序时遇到编译器错误 CS1001。

我阅读了编译器错误描述(下面的链接),但我真的不明白。我做错了什么?

http://msdn.microsoft.com/en-us/library/b839hwk4.aspx

这是我的源代码:

using System;
public class InputMethodDemoTwo
{
public static void Main()
{
int first, second;
InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0}", first);
Console.WriteLine("and second is {0}", second);
}
public static void InputMethod(out first, out second)
// The error is citing the line above this note.
{
one = DataEntry("first");
two = DataEntry("second");
}
public static void DataEntry(out int one, out int two)
{
string s1, s2;
Console.Write("Enter first integer ");
s1 = Console.ReadLine();
Console.Write("Enter second integer ");
s2 = Console.ReadLine();
one = Convert.ToInt32(s1);
two = Convert.ToInt32(s2);
}
}

根据说明,我应该有一个方法 b (InputData),它从方法 c (DataEntry) 中提取语句...以下是说明:

The InputMethod()in the InputMethodDemo program in Figure 6-24 contains repetitive code that prompts the user and retrieves integer values. Rewrite the program so the InputMethod()calls another method to do the work. The rewritten InputMethod() will need to contain only two statements:

one = DataEntry("first");

two = DataEntry("second");

Save the new program as InputMethodDemo2.cs."

The InputMethodDemo they are referring to is the same program, except that it calls only one method (the InputMethod) instead of two.

我上面提到的文本是“Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell”

任何建议/帮助将不胜感激。

最佳答案

这是你应该做的:

using System;

public class InputMethodDemoTwo
{
public static void Main()
{

int first, second;

InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0}", first);
Console.WriteLine("and second is {0}", second);
Console.ReadLine();
}

public static void InputMethod(out int first, out int second)
//Data type was missing here
{
first = DataEntry("first");
second = DataEntry("second");
}

public static int DataEntry(string method)
//Parameter to DataEntry should be string
{
int result = 0;
if (method.Equals("first"))
{
Console.Write("Enter first integer ");
Int32.TryParse(Console.ReadLine(), out result);

}
else if (method.Equals("second"))
{
Console.Write("Enter second integer ");
Int32.TryParse(Console.ReadLine(), out result);
}
return result;
}
}

关于c# - 错误 CS1001(需要标识符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4006164/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com