【问题标题】:How do I use a value after I have passed it to a new method?将值传递给新方法后如何使用它?
【发布时间】:2017-04-19 04:15:52
【问题描述】:

所以,我是一名初学者 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();


    }
}

【问题讨论】:

  • a) 你是一个业余程序员,除非你把代码手动转换成 EXE :) b) 你的返回类型是“void”,你需要使用不同的返回类型.见MSDN
  • 你不应该在这里使用数组。是的,它可以工作。但它不是正确的方法。看我的回答。

标签: c# arrays methods calculator


【解决方案1】:

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

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

【讨论】:

  • 嗯。我明白你在说什么。我确实相信这更接近我想要完成的目标。
【解决方案2】:

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

这会奏效。祝你好运

【讨论】:

    【解决方案3】:

    首先,您需要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;
    }
    

    注意 - 返回类型从voidint 的交换。

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

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

    你应该像这样声明数组:

    string[] input = new string[2];
    

    现在,你的 main 方法变成了这样:

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

    【讨论】:

    • 谢谢,帮了大忙!
    【解决方案4】:

    您需要查看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 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;
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2011-12-07
      • 1970-01-01
      • 2013-12-12
      • 2012-10-31
      • 1970-01-01
      • 2017-03-11
      相关资源
      最近更新 更多