C# 将值传递给新方法后,如何使用该值?
因此,我是一名C#程序员初学者,无法让我的程序正常工作。我希望能够使用Main()方法的用户输入,将其传递给PaintJobCalc()以计算绘制作业,并将计算结果发送回Main方法。我玩了一个小时,哪儿也去不了C# 将值传递给新方法后,如何使用该值?,c#,arrays,methods,calculator,C#,Arrays,Methods,Calculator,因此,我是一名C#程序员初学者,无法让我的程序正常工作。我希望能够使用Main()方法的用户输入,将其传递给PaintJobCalc()以计算绘制作业,并将计算结果发送回Main方法。我玩了一个小时,哪儿也去不了 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console
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();
}
}
首先,您需要
返回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();
}
现在,您的主要方法如下所示:
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();
}
您需要研究如何从函数返回值
请尝试下面的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;
}
}
由于方法的返回类型为void,因此无法返回任何内容。它不是特定于C语言的,而是在所有编程语言中都是一样的。尝试将方法PaintJobCalc的返回类型更改为int/float(以适合您的需求的为准),并在某个int/float变量上调用它
那就行了。祝你好运,你不需要数组。您的方法可以接受两个整数参数并返回整数作为结果。方法签名非常重要,它必须清楚地描述方法的输入。不要用错误的签名使事情模棱两可
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);
}
a) 你是一个业余程序员,除非你把代码手工转换成EXE:)b)你的返回类型是“void”,你需要使用不同的返回类型。看,你不应该在这里使用数组。是的,它可以工作。但这不是正确的方法。看看我的答案。谢谢,这帮了大忙!!嗯,我明白你的意思了。我相信这更接近于我想要实现的目标。