程序運行截圖如下:
每次點擊可以生成不同的成績表。
主要源代碼如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString columns[]={"姓名","語文","數學","英語","計算機"};
AnsiString names[]={"王小明","李小剛","陳大海","林小芳","張小靜","楊光"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
this->Caption="StringGrid表格簡單示例";
StringGrid1->ColCount=sizeof(columns)/sizeof(AnsiString);
StringGrid1->RowCount=1+sizeof(names)/sizeof(AnsiString);
StringGrid1->DefaultColWidth=64;
StringGrid1->ColWidths[0]=65;
for(int x=0;x<StringGrid1->ColCount;++x)
{
StringGrid1->Cells[x][0]=columns[x];
}
for(int y=1;y<StringGrid1->RowCount;++y)
{
StringGrid1->Cells[0][y]=names[y-1];
}
srand((unsigned)time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int x=1;x<StringGrid1->ColCount;++x)
{
for(int y=1;y<StringGrid1->RowCount;++y)
{
int score=60+rand()%41;
StringGrid1->Cells[x][y]=IntToStr(score);
}
}
}
//---------------------------------------------------------------------------
C++Builder(BCB)學習群(QQ)
https://www.cnblogs.com/ustone/p/16855586.html