007菜鸟猿 2022-05-25 20:51 采纳率: 66.7%
浏览 25
已结题

这个for循环中scanf函数不应该循环三次吗?为什么运行时会循环四次?

#include<stdio.h>
#include<stdlib.h>
struct SoilLayerParameter{
double VolumeWeightOfSoil;
double InternalFrictionAngle;
double CohesiveStrength;
}*SoilParameter;
struct coordinate {
double x;
double y;
}Axis[1000];
int main(void)
{
int SoilLayerNumber=0;
int i=0,j=0;
char ch='0';
printf("输入土层数\n");
scanf("%d",&SoilLayerNumber);
struct SoilLayerParameter *SoilParameter=(struct SoilLayerParameter *)calloc(SoilLayerNumber,sizeof(double));
printf("输入土层重度\n");
for(i=0;i<SoilLayerNumber;i++){
scanf("%lf\n",&(SoilParameter[i].VolumeWeightOfSoil));
}
/printf("输入土层内摩擦角");
printf("输入土层黏聚力");
double SlopeHeight=0;
scanf("%lf",&SlopeHeight);
/
while(j<SoilLayerNumber){
printf("%lf\n",SoilParameter[j].VolumeWeightOfSoil);
j++;
}
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-05-26 00:32
    关注

    修改处见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    struct SoilLayerParameter{
        double VolumeWeightOfSoil;
        double InternalFrictionAngle;
        double CohesiveStrength;
    }*SoilParameter;
    struct coordinate {
        double x;
        double y;
    }Axis[1000];
    int main(void)
    {
        int SoilLayerNumber=0;
        int i=0,j=0;
        char ch='0';
        printf("输入土层数\n");
        scanf("%d",&SoilLayerNumber);
        SoilParameter=(struct SoilLayerParameter *)calloc(SoilLayerNumber*3,sizeof(double));//修改
        //struct SoilLayerParameter *SoilParameter=(struct SoilLayerParameter *)calloc(SoilLayerNumber,sizeof(double));
        printf("输入土层重度\n");
        for(i=0;i<SoilLayerNumber;i++){
            scanf("%lf",&SoilParameter[i].VolumeWeightOfSoil); //修改 "%lf\n" 多了 '\n' .
            //scanf("%lf\n",&(SoilParameter[i].VolumeWeightOfSoil));
        }
        //printf("输入土层内摩擦角");
        //printf("输入土层黏聚力");
        //double SlopeHeight=0;
        //scanf("%lf",&SlopeHeight);/
        while(j<SoilLayerNumber){
              printf("%lf\n",SoilParameter[j].VolumeWeightOfSoil);
              j++;
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月3日
  • 已采纳回答 5月26日
  • 创建了问题 5月25日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分