2017-02-15 105 views
-2

我在制作计算器时遇到了一些麻烦。我似乎无法输入项目名称,只能输入数字。此外,它只需要最后的价格和数量,并将它们相乘而不是整体。更新:我所做的更改至小计和休息的代码,但它不断告诉我C中的二维数组计算器#

错误CS0029
无法隐式转换类型“字符串”到“字符串[]”

错误CS0019
操作“==”不能应用于operandstype'string []”和‘廉政’

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

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++)太多,但它不会工作。感谢您的提前帮助和帮助。

+0

您的名称变量是一个int,并且您将控制台输入转换为整数,所以这就是为什么只能将数字放入名称中的原因。您还在循环中设置小计=“X”,因此它将始终设置为最后一次计算。您应该使用+ =作为运行小计。 – Aaron

+0

使用'array'在哪里? – ja72

+0

我试图在while循环之前使用它,但我确信我做错了。 – Shade

回答

-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>(); 
+0

好吧,我现在明白了一点。我认为数组是完全不同的东西。我在想它是一种价值类型,而不是一个变量。感谢您的帮助。 – Shade

1

你试图名称转换为数字:

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

尝试删除 “Convert.ToInt” 是这样的:

name = Console.ReadLine(); 
+0

此外,'小计+ =价格*数量;' –