C++代码转HLA时如何实现带AND运算符的双条件While循环
HLA多条件while循环实现说明
你需要的双条件逻辑与while循环在HLA中可以直接使用&&运算符实现,和C++的用法几乎没有差异,对应代码写为:
while( (iend != 1) && (iscore < 3) ) do // 循环内逻辑 endwhile;
完整转换后的HLA代码
完全匹配你提供的C++程序逻辑的HLA代码如下:
program NumberEndsGame; #include( "stdlib.hhf" ) var inum: int32; iend: int32; iscore: int32; icheckthree: int32; icheckzero: int32; begin NumberEndsGame; // 初始化变量 mov(0, iend); mov(0, iscore); while( (iend != 1) && (iscore < 3) ) do stdout.put("请输入一个数字:"); stdin.get(inum); // 判断是否以3结尾 mov(inum, icheckthree); sub(3, icheckthree); while(icheckthree > 0) do sub(10, icheckthree); if(icheckthree == 0) then stdout.put("该数字以3结尾!", nl); inc(iscore); endif; endwhile; // 判断是否以0结尾 mov(inum, icheckzero); while(icheckzero > 0) do sub(10, icheckzero); endwhile; if(icheckzero == 0) then stdout.put("该数字以0结尾!", nl); inc(iscore); else if(icheckthree != 0) then mov(1, iend); endif; endif; // 输出本轮结果 if(iend == 1) then stdout.put(nl, "抱歉,你输了!", nl); elseif(iscore == 3) then stdout.put(nl, "恭喜你赢了游戏!", nl); else stdout.put("继续加油...", nl, nl); endif; endwhile; end NumberEndsGame;
如果需要保留原C++代码的英文输出提示,直接替换对应字符串即可。
内容的提问来源于stack exchange,提问作者JaimeVzqz
相关产品推荐
相关产品推荐

