将值传递给新方法后如何使用它?

How do I use a value after I have passed it to a new method?

所以,我是一名初学者 C# 程序员,我无法让我的程序运行。我希望能够使用 Main() 方法的用户输入,将其传递给 PaintJobCalc() 以计算绘画作业,并将计算结果发送回 main 方法。我已经玩了一个小时了,怎么也玩不转

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

class PaintingEstimate
{
    static void Main()
    {
        string[] input = {};

        Write("\n   Type a room length in feet >> ");
        input[0] = ReadLine();
        Write("\n   Type a room width in feet >> ");
        input[1] = ReadLine();

        PaintJobCalc((string[])input.Clone());

    }

    public static void PaintJobCalc(string[] args)
    {
        int inputOne = Convert.ToInt32(input[0]);
        int inputTwo = Convert.ToInt32(input[1]);

        var wallCount = (inputOne + inputTwo) * 2;
        var squareFootage = wallCount * 9;
        var endEstimate = squareFootage * 6;

        WriteLine("\n   Your Paint Job total will be [=10=]", endEstimate);

        ReadLine();


    }
}

首先,您需要return endEstimate

public static int PaintJobCalc(string[] args)
{
       int inputOne = Convert.ToInt32(args[0]);
       int inputTwo = Convert.ToInt32(args[1]);

       var wallCount = (inputOne + inputTwo) * 2;
       var squareFootage = wallCount * 9;
       var endEstimate = squareFootage * 6;
       return endEstimate;
}

注意 - return 类型从 voidint.

的交换

与某些编程语言不同 C# 数组不是动态的,您不能有一个空数组然后向其添加项并期望它们增长。

string[] input = {}; // this is size 0 and won't grow in size

你应该这样声明数组:

string[] input = new string[2];

现在,你的主要方法变成这样:

static void Main()
{
    string[] input = new string[2];

    Write("\n   Type a room length in feet >> ");
    input[0] = ReadLine();
    Write("\n   Type a room width in feet >> ");
    input[1] = ReadLine();

    int endEstimate = PaintJobCalc(input);
    WriteLine("\n   Your Paint Job total will be [=13=]", endEstimate);
    ReadLine();

}

您需要研究 this 以了解如何从函数中 return 值。

尝试以下 code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

class PaintingEstimate
{
    static void Main()
    {
        string[] input = new string[2];

        Write("\n   Type a room length in feet >> ");
        input[0] = ReadLine();
        Write("\n   Type a room width in feet >> ");
        input[1] = ReadLine();

        decimal endEstimate =PaintJobCalc((string[])input);

        WriteLine("\n   Your Paint Job total will be [=10=]", endEstimate);

        ReadLine();

    }

    public static void PaintJobCalc(string[] input)
    {
        int inputOne = Convert.ToInt32(input[0]);
        int inputTwo = Convert.ToInt32(input[1]);

        var wallCount = (inputOne + inputTwo) * 2;
        var squareFootage = wallCount * 9;
        var endEstimate = squareFootage * 6;

        return endEstimate;


    }
}

由于您方法的 return 类型无效,因此您无法 return 返回任何内容。它不是特定于 C# 的,而是在所有编程语言中都是相同的。尝试将方法 PaintJobCalc 的 return 类型更改为 int/float(以适合您的要求为准)并在某个 int/float 变量上调用它。

那行得通。祝你好运

你不需要数组。您的方法可以采用两个整数参数和 return 整数作为结果。方法签名非常重要,必须清楚地描述方法的输入。不要使用错误的签名使事情变得模棱两可。

public static int PaintJobCalc(int length, int width)
{
    var wallCount = (length + width) * 2;
    var squareFootage = wallCount * 9;
    var endEstimate = squareFootage * 6;

    return endEstimate;
}

static void Main()
{
    Write("\n   Type a room length in feet >> ");
    int length = Convert.ToInt32(ReadLine());

    Write("\n   Type a room width in feet >> ");
    int width = Convert.ToInt32(ReadLine());

    var value = PaintJobCalc(length, width);

    WriteLine("\n   Your Paint Job total will be [=10=]", value);
}