所以,我是初学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();
}
}
答案 0 :(得分:0)
首先,您需要if(names.contains("some string"))
{
//do what you wanna do here
}
。
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];
答案 1 :(得分: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 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;
}
}
答案 2 :(得分:0)
由于方法的返回类型为void,因此无法返回任何内容。它不是C#特定的,而是在所有编程语言中都是相同的。尝试将方法PaintJobCalc的返回类型更改为int / float(以适合您的要求为准)并在某个int / float变量上调用它。
那会有效。祝你好运
答案 3 :(得分: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);
}