为什么最后总是输出“A”?(标签-c++)

代码哪里出错了,刚开始学c++,不知道编码哪里出错了,希望可以帮忙指正错误

img

img

应改为90<=a&&a<=100的形式

#include<iostream>
using namespace std;
int main()
{
    int a;
    cin>>a;
    if(90<=a&&a<=100)
    cout<<"A";
    else if(80<=a&&a<90)
    cout<<"B";
    else if(70<=a&&a<80)
    cout<<"C";
    else if(60<=a&&a<70)
    cout<<"D";
    else if(a<60)
    cout<<"E";
    else
    cout<<"Error";
    return 0;
}

代码中的一个值在一个范围内,需要用&&运算符

#include<iostream>
using namespace std;
int main()
{
    int a;
    cin>>a;
    if(90<=a&&a<=100)
        cout<<"A"<<std::endl;
    else if(80<=a&&a<90)
        cout<<"B"<<std::endl;
    else if(70<=a&&a<80)
        cout<<"C"<<std::endl;
    else if(60<=a&&a<70)
        cout<<"D"<<std::endl;
    else if(a<60)
        cout<<"E"<<std::endl;
    else
        cout<<"Error"<<std::endl;
    return 0;
}

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7653934
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:第十届蓝桥杯 A组C++修改数组
  • 除此之外, 这篇博客: 手把手教会用C++实现A*算法中的 提前退出 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    广度优先搜索和 Dijkstra 算法默认会探索整张地图。如果我们只是寻找某一个目标点,我们可以增加一个判断if (current == goal),一旦我们找到目标点,便立即退出循环。

    #include "redblobgames/pathfinding/a-star/implementation.cpp"
    
    template<typename Location, typename Graph>
    std::unordered_map<Location, Location>
    breadth_first_search(Graph graph, Location start, Location goal) {
      std::queue<Location> frontier;
      frontier.push(start);
    
      std::unordered_map<Location, Location> came_from;
      came_from[start] = start;
    
      while (!frontier.empty()) {
        Location current = frontier.front();
        frontier.pop();
    
        if (current == goal) {
          break;
        }
        
        for (Location next : graph.neighbors(current)) {
          if (came_from.find(next) == came_from.end()) {
            frontier.push(next);
            came_from[next] = current;
          }
        }
      }
      return came_from;
    }
    
    int main() {
      GridLocation start{8, 7};
      GridLocation goal{17, 2};
      SquareGrid grid = make_diagram1();
      auto came_from = breadth_first_search(grid, start, goal);
      draw_grid(grid, 2, nullptr, &came_from);
    }
    

    运行结果:

    . > > > v v v v v v v v v v v v < . . . . ####. . . . . . . 
    > > > > > v v v v v v v v v v < < < . . . ####. . . . . . . 
    > > > > > v v v v v v v v v < < < < . . . ####. . . . . . . 
    > > ^ ####v v v v v v v v < < < < < < . . ####. . . . . . . 
    . ^ ^ ####> v v v v v v < ####^ < < . . . ####. . . . . . . 
    . . ^ ####> > v v v v < < ####^ ^ . . . . ##########. . . . 
    . . . ####> > > v v < < < ####^ . . . . . ##########. . . . 
    . . . ####> > > * < < < < ####. . . . . . . . . . . . . . . 
    . . . ####> > ^ ^ ^ < < < ####. . . . . . . . . . . . . . . 
    . . v ####> ^ ^ ^ ^ ^ < < ####. . . . . . . . . . . . . . . 
    . v v ####^ ^ ^ ^ ^ ^ ^ < ####. . . . . . . . . . . . . . . 
    > v v ####^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . . 
    > > > > > ^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . . 
    > > > > ^ ^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . . 
    . > > ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ####. . . . . . . . . . . . . . .
    

    从结果来看可以发现,现在算法并没有探索整张地图,而是提前结束了。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^