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

问题描述 投票:0回答:4

所以,我是一名初级 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 ${0}", endEstimate);

        ReadLine();


    }
}
c# arrays methods calculator
4个回答
0
投票

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

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 ${0}", value);
}

0
投票

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

那会起作用的。祝你好运


0
投票

首先,您需要

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;
}

注意 - 返回类型从

void
交换为
int

与某些编程语言不同

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 ${0}", endEstimate);
    ReadLine();

}

0
投票

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

尝试以下

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 ${0}", endEstimate);

        ReadLine();
        
    }

    public static double 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;


    }
}
© www.soinside.com 2019 - 2024. All rights reserved.