如何访问参数并在主程序中使用它们


using System;
namespace MyApplication
{
public class Program
{
static void CalculateCost(int length, int width, int area, int underly, 
int grippers, int grip, int fee, int carpet, int roomcarp, int total) 
{

Console.WriteLine("What's the length of your room?");

length = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("What's the width of your room?");

width = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("What area carpet would you like?");

carpet = Convert.ToInt32(Console.ReadLine());

roomcarp = area * carpet;

area = length *width;

underly = 3 * area; 

// Grippers will be required at £1/m: width * 2 + length * 2

grippers = (width * 2) + (length * 2);

grip = 1 / grippers; 

fee = 50;

total = roomcarp + underly + grippers + fee; 
}
public static void Main(string[] args)
{
//Test the program with a 7 x 6m room and a £17m2 carpet. E.g. 7m x 
6m room with a £17 carpet = £714 + £126 + £26 + £50 = £916
Console.WriteLine("{0} x {1} room with {2} carpet. = {3} + {4} + {5} + 
{6} = {7} = {8} ", length, width, carpet, roomcarp, underly, grippers, 
fee, total);


}  
}
}

尝试这个

public static void Main(string[] args)
{

CalculateCost(out var length, out var width, out var carpet, out var roomcarp, 
out var area, out var underly, out var grippers, out var grip, out var fee, out var total);
Console.WriteLine("{0} x {1} room with {2} carpet. = {3} + {4} + {5} +   {6} = {7} = {8}",
length, width, carpet,area, roomcarp, underly,grippers, fee, total);
}

public static void CalculateCost(out int length, out int width, out int carpet, out int roomcarp, out int area, out int underly,
out int grippers, out double grip, out double fee, out double total)
{
Console.WriteLine("What's the length of your room?");
length = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Length = " + length);
Console.WriteLine("What's the width of your room?");
width = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Width = " + width);
Console.WriteLine("What area carpet would you like?");
carpet = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Carpet = " + carpet);

area = length * width;
roomcarp = area * carpet;
underly = 3 * area;
// Grippers will be required at £1/m: width * 2 + length * 2
grippers = (width * 2) + (length * 2);
grip = 1 / grippers;
fee = 50;
total = roomcarp + underly + grippers + fee;
}

最新更新