如果计数器是 >9,那么我想打印一个字母等于字母中的计数器



我正在制作一个nonogram,但有一个要求是,当计数器大于9时,我希望打印字母,所以例如,如果计数器为10,我想打印a、11-B等。这是我的函数,值hulp被放在另一个函数的数组中。

int nono::letter(int counter){
char hulp;
if (counter > 9){
return hulp ='a'+counter;
}
}

我把它放在的功能:

void nono::makeDis (){
bool empty = false;
int Dis;
int counter = 0;
for (int j = 0; j <= width; j++){
for (int i = 0; i <= height; i++){
column[j][i] = 0;
}
}
for (int j = 0; j <= width; j++){
besc = 0;
counter=0;
for (int i = 0; i <= height; i++){
if (denono[i][j]==1){
counter++;
}else if(counter > 0 && counter <10){
column[j][besc]=counter;
besc++;
counter = 0;
}else if(counter>9){
column[j][besc]=letter(counter);
besc++;
counter=0;
}
}
}
}

这就是我打印阵列的方式:

void nono::printDescColumn(){
bool notEmpty;
int x =0;

cout << "   ";
for (int j = 0; j < width; j++) {
controlBeschKolom(j);
if(valideK[j]) {
cout << "V ";
}
else{
cout << "  ";
}
}
cout << endl;
for (int i = 0; i < height; i++){
notEmpty=false;
cout<< "   ";
for (int j = 0; j < width; j++){
///controlBeschKolom(j);
if(column[j][i]!=0 ){
cout<<column[j][i]<<" ";
notEmpty=true;
}else{
cout<<"  ";
}
}
cout<<endl;
if(!notEmpty){
break;
}
}
}

我看到的所以我想要一个而不是75

在将计数器添加到"a"之前,需要先从计数器中减去10,以确保使用正确的偏移量。既然你已经在检查呼叫代码中的号码us是否在9以上,你就不需要在这里做了。因此,您的代码简化为以下内容:

char nono::letter(int counter){
return 'A'+counter-10;   
}

此外,在打印函数中,确保在打印列数组值之前将其转换为char,如下所示:

void nono::printDescColumn(){
bool notEmpty;
int x =0;
cout << "   ";
for (int j = 0; j < width; j++) {
controlBeschKolom(j);
if(valideK[j]) {
cout << "V ";
}
else{
cout << "  ";
}
}
cout << endl;
for (int i = 0; i < height; i++){
notEmpty=false;
cout<< "   ";
for (int j = 0; j < width; j++){
///controlBeschKolom(j);
if(column[j][i]!=0 ){
if(column[j[[i]>9) {
cout<<char(column[j][i]<<" ";
}else{
cout<<column[j][i]<<" ";
}
notEmpty=true;
}else{
cout<<"  ";
}
}
cout<<endl;
if(!notEmpty){
break;
}
}

}

hulp变量的用法已过时,会使代码看起来可疑。你为什么不这样做呢:

char nono::letter(int counter){
if (counter > 9){
return 'A'+counter;
}
else return '0' + counter;
}

最重要的是,你提到了你想看到的东西,你展示了你迄今为止创建的程序,但当你运行该程序时,你实际上看到了什么?(这可能有助于我们了解您的问题所在(顺便问一下,是什么问题?(

最新更新