c++ - 如何在数组表中输出水平字母
问题描述
我正在制作一个 6x6 骰子游戏,我需要能够将 ROWS 放在桌子的 y 轴上,但我不明白如何用我的代码来做到这一点。
创建一个 6x6 2D 表,其中包含使用值 1 到 6 的行和列的总和。
我一直在阅读 Tony Gaddis 的第九版“从控制结构到对象的 C++ 入门”,但我找不到任何关于我正在寻找的东西。
//System Libraries
#include <iostream> //Input/Output Library
#include <iomanip> //Format Library
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
const int COLS=7;
//Function Prototypes
void fillTbl(int [][COLS],int);
void prntTbl(const int [][COLS],int);
//Execution Begins Here!
int main(int argc, char** argv) {
//Declare Variables
const int ROWS=6;
int tablSum[ROWS][COLS] ={{1,2,3,4,5,6,7},
{2,3,4,5,6,7,8},
{3,4,5,6,7,8,9},
{4,5,6,7,8,9,10},
{5,6,7,8,9,10,11},
{6,7,8,9,10,11,12}};
//Initialize or input i.e. set variable values
fillTbl(tablSum,ROWS);
cout<<"Think of this as the Sum of Dice Table\n";
cout<<" C o l u m n s\n";
cout<<" | 1 2 3 4 5 6\n";
cout<<"----------------------------------\n";
//Display the outputs
prntTbl(tablSum,ROWS);
//Exit stage right or left!
return 0;
}
void fillTbl(int tablSum [][COLS],int ROWS)
{
cout<<"";
}
void prntTbl(const int tablSum [][COLS],int ROWS)
{
for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < COLS; y++)
{
cout<<setw(4)<<tablSum[x][y];
}
cout<<endl;
}
}
Your Output
Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1···2···3···4···5···6···7↵
···2···3···4···5···6···7···8↵
···3···4···5···6···7···8···9↵
···4···5···6···7···8···9··10↵
···5···6···7···8···9··10··11↵
···6···7···8···9··10··11··12↵
Expected Output
Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1·|···2···3···4···5···6···7↵
R··2·|···3···4···5···6···7···8↵
O··3·|···4···5···6···7···8···9↵
W··4·|···5···6···7···8···9··10↵
S··5·|···6···7···8···9··10··11↵
···6·|···7···8···9··10··11··12↵
解决方案
我们可以将您的 prntTbl 函数更改为包含行字符串的字符串文字:char* rows = " ROWS ";
然后在每次内部循环迭代之前,我们可以使用我们的第一个循环索引以及行值和任何间距打印字符串索引处的字符使用:cout << rows[x] << " " << x + 1 << " |";
我们结束的 prntTbl 方法如下所示:
void prntTbl(const int tablSum [][COLS],int ROWS)
{
char* rows = " ROWS ";
for(int x = 0; x < ROWS; x++)
{
cout << rows[x] << " " << x + 1 << " |";
for(int y = 0; y < COLS; y++)
{
cout<<setw(4)<<tablSum[x][y];
}
cout<<endl;
}
和输出:
C o l u m n s
| 1 2 3 4 5 6
----------------------------------
1 | 1 2 3 4 5 6 7
R 2 | 2 3 4 5 6 7 8
O 3 | 3 4 5 6 7 8 9
W 4 | 4 5 6 7 8 9 10
S 5 | 5 6 7 8 9 10 11
6 | 6 7 8 9 10 11 12
推荐阅读
- javascript - 来自 responseText 的 innerHTML:如何替换特殊字符?
- powerbi - Dax 表达式等效于 ISNOTBLANK()
- swift - Swift 中的 VPN - loadFromPreferences 未运行处理程序
- r - 画一个圆圈以突出显示 R 中曲线的最大值
- kentico - Kentico - 未能验证安全标头
- wolfram-mathematica - Mathematica:保持不变
- dart - 如何访问从 Google 的 flutter_chart 库中的 BaseChart 继承的属性?
- spring-data-jpa - 春季jpa中的deleteById与删除
- javascript - 带有 AJAX 的 OAuth2 令牌
- jenkins - 无法在 Jenkins slave 上执行 Ant 构建工具