2017-02-15 108 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

此外,'小計+ =價格*數量;' –