gpt4 book ai didi

c++ - 运行 while 的第一个实例

转载 作者:行者123 更新时间:2023-11-28 03:42:29 26 4
gpt4 key购买 nike

作业:

Write a program that loops indefinitely. In each iteration of the loop, read in an integer N (declared as an int) that is entered by a user, display N/5 if N is non-negative and divisible by 5, or -1 otherwise. Use the ternary operator (?:) to accomplish this. (Hint: the modulus operator may be useful.)

我的解决方案:

#include<iostream>
using namespace std;
int main(){
int x;
cin>>x;
while(1) {
cin>>x;
int result;
cout<<" "<<endl;
result = (x>0 & (x%5==0)) ? int(x/5) : -1;
cout<<result;
}
}

我可以做题但是程序的第一次运行没有给出输出

最佳答案

逐行检查程序。使用 cin>>x,您可以将一个数字读入 x。第 6 行是一个 while (1),1 为真,所以你进入了循环。接下来的 cin>>x 将一个数字读入 x,覆盖之前的内容。

关于c++ - 运行 while 的第一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com