2017-10-20 3 views
0

all。ここの学生プログラマーは、悩みだけではなく、配列に苦労しています。私は数週間前にポイントを半分にして宿題を割り当てました。なぜなら、私は並列アレイを動作させることができなかったからです。私たちは、6つの異なる市外局番の電話料金を計算するためのGUIを作成するよう求められました。 GUIは市外局番(あなたが入力する有効なコードのリストを得る)と呼の長さを要求する。私の問題は、プログラムをエリアコード配列をループさせることにあると思いますが、ここからどこに行くのかはまったく分かりません。 (私は答えが何であるかを見ると、私は褒め言葉になるだろうと思う)。ここに私のGUIボタンのコードがある。私が入力した市外局番にかかわらず、$ 1.40の費用を返します。見てくれてありがとう!あなたは、あなたがあなたの現在のコードに必要なすべての条件が追加されたループから抜け出す方法を示さなければならないの割り当てを与えられている場合C# - パラレルアレイを使用してGUIで電話のコストを計算する

private void calcButton_Click(object sender, EventArgs e) 
    { 
     int[] areaCode = { 262, 414, 608, 715, 815, 902 }; 
     double[] rates = { 0.07, 0.10, 0.05, 0.16, 0.24, 0.14 }; 
     int inputAC; 
     double total = 0; 

     for (int x = 0; x < areaCode.Length; ++x) 
     { 

      inputAC = Convert.ToInt32(areaCodeTextBox.Text); 

      total = Convert.ToInt32(callTimeTextBox.Text) * rates[x]; 
      costResultsLabel.Text = "Your " + callTimeTextBox.Text + "-minute call to area code " + areaCodeTextBox.Text + " will cost " + total.ToString("C"); 


     } 
    } 
+0

単一の市外局番の結果が必要な場合、なぜ市外局番をループしていますか? – progrAmmar

+0

'areaCode'配列の' inputAC'のインデックスを見つける必要があります。 –

+0

@progrAmmar、素晴らしい点。私は今私の質問をどのように誤解したかを見ています。私は、エリアコードの配列を索引付けするのに問題があると言います。 –

答えて

0

、代わりにこの

private void calcButton_Click(object sender, EventArgs e) 
{ 
    int[] areaCode = { 262, 414, 608, 715, 815, 902 }; 
    double[] rates = { 0.07, 0.10, 0.05, 0.16, 0.24, 0.14 }; 
    if(!string.IsNullOrEmpty(areaCodeTextBox.Text)) 
    { 
     double total = 0; 
     if(!string.IsNullOrEmpty(callTimeTextBox.Text)) 
     { 
      int index = Array.IndexOf(areaCode, int.Parse(areaCodeTextBox.Text)); //You can use TryParse to catch for invalid input 
      if(index > 0) 
      { 
       total = Convert.ToInt32(callTimeTextBox.Text) * rates[index]; 
       costResultsLabel.Text = "Your " + callTimeTextBox.Text + "-minute call to area code " + areaCodeTextBox.Text + " will cost " + total.ToString("C"); 
      } 
      else 
      { 
       //Message Area code not found 
      } 
     } 
     else 
     { 
      //Message Call time is empty 
     } 
    } 
    else 
    { 
     //Message Area Code is empty 
    } 
} 

をお試しください

private void calcButton_Click(object sender, EventArgs e) 
{ 
    int[] areaCode = { 262, 414, 608, 715, 815, 902 }; 
    double[] rates = { 0.07, 0.10, 0.05, 0.16, 0.24, 0.14 }; 
    int inputAC; 
    double total = 0; 

    for (int x = 0; x < areaCode.Length; ++x) 
    {  
     inputAC = Convert.ToInt32(areaCodeTextBox.Text); 
     total = Convert.ToInt32(callTimeTextBox.Text) * rates[x]; 

     if(inputAC == areaCode[x]) //ADDED condition 
      break; 
    } 
    costResultsLabel.Text = "Your " + callTimeTextBox.Text + "-minute call to area code " + areaCodeTextBox.Text + " will cost " + total.ToString("C"); 
} 
+0

ありがとう。現在のコードに条件/ブレークを追加し、それが機能しました。エラーメッセージのコンボに取り組んでください。それはTryParseなどを実践する優れた方法です。 –

関連する問題