我的代码
int main(void){
int arr1[6],arr2[6];
const char * weather[] = {"January","February","March","April","May","June"};
printf("Enter 6 numbers for Arr1"); //get values for arr1
for(int i = 0;i<6;i++){
scanf("%d",&arr1[i]);
//avg2 = intarr[i] + avg2;
}
printf("Enter 6 numbers for Arr1"); //get values for arr2
for(int i = 0;i<6;i++){
scanf("%d",&arr2[i]);
//avg2 = intarr[i] + avg2;
}
for(int i = 0;i<6;i++){ . //print values for arr1
printf("\n%s ",weather[i]);
printf("|");
for(int k = 0;k<arr1[i];k++){
printf("*");
}
printf("\n |");
for(int k = 0;k<arr2[i];k++){
printf("!");
}
}
}
输出: https://i.imgur.com/GymyHqD.png 我对如何使2月的行正确格式化感到困惑。如图所示,其他选项卡通过一个选项卡将其关闭。有没有简便的格式化方法?
答案 0 :(得分:4)
为什么不printf
用长度修饰符对字符串左对齐。
printf("%-20s", weather[i]);
将打印weather[i]
:
-
使它左对齐20
使字段长度为20个字符。空的地方到处都是空白。