0

ボタンがクリックされたときに、配列の内容を取得してメッセージ ボックスに出力する必要があります。ユーザーが追加ボタンを押すと、数値が配列に読み込まれ、他の機能は問題なく実行されます。ただし、表示ボタンをクリックすると、メッセージ ボックスが表示されますが、0 と表示されます。コードは次のとおりです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Testscores.UI
{
    public partial class frmTestscores : Form
    {
        //creates the variables needed for the calculations
        int scores;
        double total = 0;
        int count = 0;
        int counts = 0;
        double average = 0;
        int[] sArray;


        public frmTestscores()
        {
            InitializeComponent();
        }
        //creates the exit button click event
        private void btnExit_Click(object sender, EventArgs e)
        {
            //exits the application
            Application.Exit();
        }
        //creates the clear button click event
        private void btnClear_Click(object sender, EventArgs e)
        {
            //clears all text fields and variables
            txtAverage.Text = "";
            txtCount.Text = "";
            txtScore.Text = "";
            txtTotal.Text = "";
            scores = 0;
            total = 0;
            counts = 0;
            average = 0;

        }
        //creates the add button click event
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //creates the try and catch statements
            try
            {
                //does the calculations and outputs it into the text fields
                scores = int.Parse(txtScore.Text);

                counts += 1;
                total = total + scores;

                average = total / counts;
                    txtAverage.Text = average.ToString();
                    txtTotal.Text = total.ToString();
                txtCount.Text = counts.ToString();
                //initializes the array(this is where i get confused)
                int SIZE = counts;
                Array.Resize(ref sArray, SIZE);
                for (int count = 1; count > SIZE; count++)
                {

                   sArray[count] = scores;
                    //outputs the count to the text box
                    txtCount.Text = count.ToString();




                }
            }
                //catch statement
            catch (Exception ex)
            {
                //outsputs a message to the user
                MessageBox.Show("Please enter a valid number,");

            }
        }
        //creates the display button click event
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            //supposed to output the array to a message box
            MessageBox.Show(sArray[].ToString());

        }
    }
}
4

3 に答える 3

5

配列の個々の文字列を (string.Join メソッドなどを使用して) 単一の文字列に結合し、連結された文字列を表示できます。

string toDisplay = string.Join(Environment.NewLine, sArray); 
MessageBox.Show(toDisplay);
于 2012-09-20T20:43:13.293 に答える
0

ディスプレイ クリック イベント ハンドラを次のように変更します。

private void btnDisplay_Click(object sender, EventArgs e)
{
    string output = string.Empty;
    foreach (var item in sArray)
    {
        output += item + " ";
    }

    //supposed to output the array to a message box
    MessageBox.Show(output);
}
于 2012-09-20T20:44:19.800 に答える
0

はい、これは配列全体を出力しません。ToString() のドキュメントを読んでください。サブクラスでオーバーライドされない限り、通常はオブジェクトの型名を出力します。

これを行う強引な方法は非常に単純です。

string output = new string();

for(int i = 0; i < sArray.Length; i++)
{
   output += sArray[i] // plus any delimiters or formating.
}

MessageBox.Show(output);
于 2012-09-20T20:44:38.690 に答える