尝试在 C# 程序中为平均股票计算器定义两个数字时出现问题
promblem while trying to define two numbers in c# programe for average stock calculator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace average_stock_calculator
{
class Program
{
static void Main(string[] args)
{
decimal a, b, c, d, sum, average;
Console.WriteLine("First purchase price:");
a = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Second purchase price:");
b = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Third purchase price:");
c = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Fouth purchase price:");
d = Convert.ToDecimal(Console.ReadLine());
//Processing
sum = a + b + c + d;
average = sum/4;
Console.WriteLine("Average buying price={0}", average);
Console.ReadKey();
}
}
}
注意:我想把首购数量和首购价格加在一起,不知道怎么办!有人可以帮忙吗?
它应该看起来像这样,
输入首次购买数量和价格
我应该能够一起输入数量 10 和价格 20,然后最后我应该得到平均购买数量和价格。
我给你做了简单的修改
代码:
using System;
namespace ConsoleApp9
{
internal class Program
{
static void Main(string[] args)
{
Purchase a, b, c, d;
Console.WriteLine("First purchase price and amount:");
a = ReadL();
Console.WriteLine("Second purchase price and amount:");
b = ReadL();
Console.WriteLine("Third purchase price and amount:");
c = ReadL();
Console.WriteLine("Fouth purchase price: and amount:");
d = ReadL();
//Processing
Purchase Sum = new Purchase();
Sum.price = a.price + b.price + c.price + d.price;
Sum.amount = a.amount + b.amount + c.amount + d.amount;
Purchase Average = new Purchase();
Average.price = Sum.price / 4;
Average.amount = Sum.amount / 4;
Console.WriteLine("Average buying price={0},Average buying amount={1}", Average.price, Average.amount);
Console.ReadKey();
Purchase ReadL()
{
Purchase X = new Purchase();
var inputs = Console.ReadLine().Split(' ');
X.amount = Convert.ToDecimal(inputs[0]);
X.price = Convert.ToDecimal(inputs[1]);
return X;
}
}
class Purchase
{
public decimal price { get; set; }
public decimal amount { get; set; }
}
}
}
输出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace average_stock_calculator
{
class Program
{
static void Main(string[] args)
{
decimal a, b, c, d, sum, average;
Console.WriteLine("First purchase price:");
a = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Second purchase price:");
b = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Third purchase price:");
c = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Fouth purchase price:");
d = Convert.ToDecimal(Console.ReadLine());
//Processing
sum = a + b + c + d;
average = sum/4;
Console.WriteLine("Average buying price={0}", average);
Console.ReadKey();
}
}
}
注意:我想把首购数量和首购价格加在一起,不知道怎么办!有人可以帮忙吗? 它应该看起来像这样, 输入首次购买数量和价格 我应该能够一起输入数量 10 和价格 20,然后最后我应该得到平均购买数量和价格。
我给你做了简单的修改
代码:
using System;
namespace ConsoleApp9
{
internal class Program
{
static void Main(string[] args)
{
Purchase a, b, c, d;
Console.WriteLine("First purchase price and amount:");
a = ReadL();
Console.WriteLine("Second purchase price and amount:");
b = ReadL();
Console.WriteLine("Third purchase price and amount:");
c = ReadL();
Console.WriteLine("Fouth purchase price: and amount:");
d = ReadL();
//Processing
Purchase Sum = new Purchase();
Sum.price = a.price + b.price + c.price + d.price;
Sum.amount = a.amount + b.amount + c.amount + d.amount;
Purchase Average = new Purchase();
Average.price = Sum.price / 4;
Average.amount = Sum.amount / 4;
Console.WriteLine("Average buying price={0},Average buying amount={1}", Average.price, Average.amount);
Console.ReadKey();
Purchase ReadL()
{
Purchase X = new Purchase();
var inputs = Console.ReadLine().Split(' ');
X.amount = Convert.ToDecimal(inputs[0]);
X.price = Convert.ToDecimal(inputs[1]);
return X;
}
}
class Purchase
{
public decimal price { get; set; }
public decimal amount { get; set; }
}
}
}
输出: