第一次发布,并对是否带有方法的语句有疑问。
我一直在尝试多种方法来做到这一点,但是还没有使它起作用。我正在尝试根据用户是否键入1到6之间的数字来调用方法。这是我到目前为止的结果,请不要判断计算器的对话框(我只有14岁)。
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Work());
}
public static void Work(int input)
{
if (input < 0)
{
Console.WriteLine("Please enter a number between 1 and 6");
}
else if (input == 1)
{
Console.WriteLine(Add());
}
else if (input == 2)
{
Console.WriteLine(Subtract());
}
}
public static int Add()
{
Console.WriteLine("Hey bro, need a new number man!");
string input1 = Console.ReadLine();
Console.WriteLine("Gnarly dude, how 'bout a second one?");
string input2 = Console.ReadLine();
Console.WriteLine("Here botine-shake, there is your final number.");
int num1 = int.Parse(input1);
int num2 = int.Parse(input2);
int result = num1 + num2;
return result;
}
public static int Subtract()
{
Console.WriteLine("Number. Now. Please hurry.");
string input1 = Console.ReadLine();
Console.WriteLine("Need another number. Hurry.");
string input2 = Console.ReadLine();
Console.WriteLine("Here is your number. Now please leave.");
int num1 = int.Parse(input1);
int num2 = int.Parse(input2);
int result = num1 - num2;
return result;
}
public static int Multiply()
{
Console.WriteLine("Gimme that number.");
string input1 = Console.ReadLine();
Console.WriteLine("Ok, how would you like to give me another?");
string input2 = Console.ReadLine();
Console.WriteLine("Here you go baby.");
int num1 = int.Parse(input1);
int num2 = int.Parse(input2);
int result = num1 * num2;
return result;
}
public static decimal Divide()
{
Console.WriteLine("/enter.Num1");
string input1 = Console.ReadLine();
Console.WriteLine("/enter.Num2");
string input2 = Console.ReadLine();
Console.WriteLine("/final.Dividend");
decimal num1 = decimal.Parse(input1);
decimal num2 = decimal.Parse(input2);
decimal result = num1 / num2;
return result;
}
public static int Square()
{
Console.WriteLine("What number do you want SQUARED?");
string input1 = Console.ReadLine();
Console.WriteLine("Here's your square!");
int num1 = int.Parse(input1);
int result = num1 * num1;
return result;
}
public static int Cube()
{
Console.WriteLine("What have thine want CUBED?");
string input1 = Console.ReadLine();
Console.WriteLine("Here, I bestow upon you your cube...");
int num1 = int.Parse(input1);
int result = num1 * num1 * num1;
return result;
}
}
答案 0 :(得分:0)
我不太明白您的意思,但是应该修复一些问题:
static void Main(string[] args)
{
Console.WriteLine(Work());
}
public static void Work(int input)
......
您可以看到Work()
是一个“动作”,而不是一个“功能”。由于它不返回任何内容,因此不适合WriteLine
的结果Work()
顺便说一句,您可以看到Work(int input)
需要输入,显然您没有提供。
试试这个:
static void Main(string[] args)
{
int myInput = int.Parse(Console.ReadLine());
Work(myInput);
}
public static void Work(int input)
{ .....
答案 1 :(得分:0)
根据您提供的代码,看起来您在调用Work方法之前没有要求用户输入,因此不会发生任何事情,因为没有参数传递给Work方法。
您应该尝试这样的事情:
static void Main(string[] args)
CaptureUserInput();
}
public static void WriteInstructions() {
Console.WriteLine("Enter a number between 1 and 6 to perform a calculation");
// instruct the user for each available operation
Console.WriteLine("1: Addition");
Console.WriteLine("2: Subtraction");
}
public static void CaptureUserInput() {
WriteInstructions();
// capture the user's input and convert it to an integer
string stringInput = Console.Readline();
int input = int.Parse(stringInput);
// validate that it is a valid integer
if (Enumerable.Range(1,6).Contains(input)) {
// this is a valid number in the range we want, call the Work method
Work(input);
} else {
// the user has entered an invalid entry, prompt them and wait for another attempt
Console.WriteLine("Sorry, that is an invalid option.");
CaptureUserInput();
}
}
请注意,此设置是为了使您可以在用户输入的数字不是数字或超出1-6的范围时递归处理。这将提示用户输入的内容无效,然后等待下一次尝试输入一个数字。