aaronbin 2019-07-02 16:55 采纳率: 0%
浏览 244

关于C++的一个小问题(循环方面)

当city数组中的字符串数与数据相同时会显示,但是当字符串数小于数据时则不会进入循环体,但是循环的判断条件为true,但是不执行里面的语句,以下代码是我测试的

#include <stdio.h>
#include <iostream>
const int Cities = 5;
const int Years = 4;
int main()
{
    using namespace std;
    const char* cities[Cities] = 
    {
        "Gribble City",
        "Gribbletown",
        //"New Gribble",
        //"San Gribble",
        "Gribble Vista"
    };

    int maxtemps[Years][Cities] = {
        {96, 100, 87, 101, 105},
        {96, 98, 91, 107, 104},
        {97, 101, 93, 108, 107},
        {98, 103, 95, 109, 108}
    };

    cout << "Maximum tempertures for 2008 - 2011\n\n";
    for (int city = 0; city < Cities; ++city)
    {
        if (city == 4)
            cout << "You have found me." << endl;
        cout << cities[city] << ":\t";

        for (int year = 0; year < Years; year++)   
            cout << maxtemps[year][city] << "\t";
            cout << endl;
    }

    system("pause");
    return 0;
}

实际只输出前三个数组的数据

  • 写回答

2条回答 默认 最新

  • Italink 2019-07-03 02:15
    关注
    #include <stdio.h>
    #include <iostream>
    const int Cities = 5;
    const int Years = 4;
    int main()
    {
        using namespace std;
        const char* cities[Cities] =
        {
            "Gribble City",
            "Gribbletown",
            //"New Gribble",
            //"San Gribble",
            "Gribble Vista"
        };
    
        int maxtemps[Years][Cities] = {
            {96, 100, 87, 101, 105},
            {96, 98, 91, 107, 104},
            {97, 101, 93, 108, 107},
            {98, 103, 95, 109, 108}
        };
    
        cout << "Maximum tempertures for 2008 - 2011\n\n";
        for (int city = 0; city < Cities; ++city)
        {
            if (city > 2)
                cout << "error" << endl;
            else
                cout << cities[city] << ":\t";
    
            for (int year = 0; year < Years; year++)
                cout << maxtemps[year][city] << "\t";
            cout << endl;
        }
    
        system("pause");
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。