2017-02-15 27 views
-2

電卓を作るのに少し問題があります。私は項目名を入力することができないように見えますが、数字だけです。また、最後の価格と数量しかかかりません。アップデート:私は、小計や休憩に関する規定を変更したが、それは私に言って続けて2次元配列計算機C#


が暗黙的に型「string」を変換できません。エラーCS0029「文字列[]」

エラーCS0019
演算子「==」は[、] operandstype'stringに適用することができない」と '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++)を持っている必要があることを知っているが、それは動作しません。お時間をいただきありがとうございました。

+0

あなたの名前変数はintであり、コンソール入力を整数に変換するので、その名前のためだけに数字を入れることができます。また、ループ内でsubtotal = "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)があります。値が(string、int、decimal)のタイプをそれぞれ格納する3つの配列があります。後で、それぞれが複数の値を含む構造体の単一の配列を作る方法を学ぶ必要があります。

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

また、 'subtotal + = price * quantity;' –