C#中的2d数组计算器

时间:2017-02-15 19:15:53

标签: c# arrays

我在制作计算器时遇到了一些麻烦。我似乎无法输入项目名称,只能输入数字。此外,它只需要最后的价格和数量,并将它们乘以而不是整体。更新:我对代码进行了关于小计和休息的更改,但它一直告诉我

  

错误CS0029
  无法将类型'string'隐式转换为'string []'

     

错误CS0019
  运算符'=='不能应用于operandstype的字符串[]'和'int'

我似乎无法使其工作或添加列表到最后。任何帮助,将不胜感激。

int[] array;
string[,] name = new string[100, 4];
decimal counter;

decimal price;
decimal subtotal;
decimal tax;
decimal total;
decimal quantity;

subtotal = 0;
counter = 0;

array = new int[5];
while (counter <= 10)
{
    Console.Write("Item{0}", counter + 1);
    Console.Write("        Enter item name: ");
    name = Console.ReadLine();
    if (name == 0)
        break;
    Console.Write("        Enter price: ");
    price = Convert.ToDecimal(Console.ReadLine());


    counter = counter + 1;

    Console.Write("        Enter quantity: ");
    quantity= Convert.ToInt32(Console.ReadLine());
    subtotal += price * quantity;
}
Console.WriteLine("-------------------");
Console.WriteLine("\nNumber of Items:{0}", counter);
Console.WriteLine("Subtotal is {0}", subtotal);
tax = subtotal * 0.065M;
Console.WriteLine("Tax is {0}", tax);
total = tax + subtotal;
Console.WriteLine("Total is {0}", total);
Console.WriteLine("Thanks for shopping! Please come again.");
Console.Read();

我也知道我需要在代码中使用for ( int counter = 0; counter < array.Length; counter++ ),但它不起作用。感谢您的时间和提前帮助。

2 个答案:

答案 0 :(得分:1)

您正在尝试将名称转换为数字:

name = Convert.ToInt32(Console.ReadLine());
            if (name == 0)
                break;

尝试删除&#34; Convert.ToInt&#34;像这样:

name = Console.ReadLine();

答案 1 :(得分:-1)

也许这个程序可以帮助你学习C#。但你必须承诺试图了解它的工作原理和原因。此外,使用调试器进行操作以查看每个语句如何影响存储在变量中的值。

class Program
{
    static void Main(string[] args)
    {
        const int max_count = 10;

        string[] name = new string[max_count];
        int[] quantity = new int[max_count];
        decimal[] price = new decimal[max_count];
        decimal subtotal = 0;
        int count = 0;
        // Enter Values
        for(int i = 0; i<max_count; i++)
        {
            Console.Write("Item{0}", i+1);
            Console.Write("\tEnter Item Name: ");
            name[i]=Console.ReadLine();
            if(name[i].Length==0)
            {
                break;
            }
            Console.Write("\tEnter Price: ");
            price[i]=decimal.Parse(Console.ReadLine());
            Console.Write("\tEnter Quantity: ");
            quantity[i]=int.Parse(Console.ReadLine());
            subtotal+=quantity[i]*price[i];
            count+=1;
        }
        // Display Summary
        Console.WriteLine("-------------------");
        Console.WriteLine("\nNumber of Items:{0}", count);
        Console.WriteLine("Subtotal is {0}", subtotal);
        decimal  tax=subtotal*0.065M;
        Console.WriteLine("Tax is {0}", tax);
        decimal total=tax+subtotal;
        Console.WriteLine("Total is {0}", total);
        Console.WriteLine("Thanks for shopping! Please come again.");
        Console.Read();
    }
}

这里有一个数组的结构(Program)。有三个数组,每个数组存储不同的类型值(字符串,整数,十进制)。稍后你应该学会制作一个结构的单个数组,每个数组包含多个值。

public class Item
{
    public string name;
    public decimal price;
    public int quantity;
}

// Usage
var cart = new List<Item>();