我是C ++的新手,我在过去2小时内一直坚持不知所措。我只能使用iostream库。 其中一个要求是在输入0时取消循环,然后一旦循环停止,我需要让它显示偶数,奇数,最大数和输入的整数总数。
任何帮助将不胜感激!
#include <iostream>
using namespace std;
int main()
{
int number,
integer,
even = 0,
odd = 0,
max,
total;
do {
cout << "Type an integer (type 0 to stop):";
cin >> number;
}
while (number != 0);
{
if (number % 2 == 0)
even++;
else
odd++;
}
cout << "# of evens = " << even;
cout << "# of odds = " << odd;
cout << "max = ";
cout << "number of integers entered is ";
return 0;
}
答案 0 :(得分:4)
计算偶数和奇数,最大数量和数量的要求不需要存储用户输入的值。所以你需要在循环中完成所有这些。
do .. while
语法如下:
do {
// all the loop operations go here
} while (condition);
// here the loop is over !!! it's executed AFTER, when condition is false
您使用的语法无法正常工作:
do {
// all the loop operations go here
} while (condition); // semi-column means "end of statement"
{
// ...this is a block of statements. It's independent of the while
// so it will still only be executed when the loop is over.
}
不幸的是,您的do..while
循环只有输入和输出。数字的处理目前在循环之外完成。所以它只会执行一次:当用户输入0时。
重做你的循环:
do {
cout << "Type an integer (type 0 to stop):";
cin >> number;
if (number !=0 ) {
// .. put all your processing and counting here
}
} while (number != 0);
当你学习IT时,也许有一天我可以选择飞机的飞行计算机,我更愿意让你在循环中完成你的运动,以确保你完全理解你所谓的知道。
答案 1 :(得分:1)
虽然这类问题通常不属于SO,但你试了一下,并展示了一些可修复的问题:
答案 2 :(得分:0)
SPOIL(如果你不想要解决方案,请不要阅读)(c-like):
#include <iostream>
struct stats {
int max;
int total;
unsigned int counter;
unsigned int even;
unsigned int odd;
};
static void display(struct stats const &stats) {
std::cout << "counter = " << stats.counter << '\n';
std::cout << "even = " << stats.even << '\n';
std::cout << "odd = " << stats.odd << '\n';
std::cout << "max = " << stats.max << '\n';
std::cout << "total = " << stats.total << '\n';
}
static int ask_number(void) {
int number;
std::cout << "Type an integer (type 0 to stop):";
std::cin >> number;
return number;
}
static void do_stats(int const number, struct stats &stats) {
if (number % 2 == 0) {
stats.even++;
}
else {
stats.odd++;
}
stats.total += number;
stats.counter++;
if (number > stats.max) {
stats.max = number;
}
}
int main(void) {
int number = ask_number();
struct stats stats = {number, 0, 0, 0, 0};
while (number != 0) {
do_stats(number, stats);
number = ask_number();
}
display(stats);
return 0;
}
SPOIL(如果你不想要解决方案,请不要阅读)(true cpp):
#include <iostream>
class Number {
public:
static int ask(std::ostream &os, std::istream &is) {
int number;
os << "Type an integer (type 0 to stop):";
is >> number;
return number;
}
void ask_and_replace(std::ostream &os, std::istream &is) {
number = ask(os, is);
}
Number(std::ostream &os, std::istream &is) :
number(ask(os, is))
{
}
public:
int number;
};
class Stats {
public:
Stats(int max) :
max(max),
total(0),
counter(0),
even(0),
odd(0)
{
}
private:
int max;
int total;
unsigned int counter;
unsigned int even;
unsigned int odd;
public:
friend std::ostream& operator<<(std::ostream& os, Stats const &stats);
void run(int const number) {
if (number % 2 == 0) {
even++;
}
else {
odd++;
}
total += number;
counter++;
if (number > max) {
max = number;
}
}
};
std::ostream& operator<<(std::ostream& os, Stats const &stats)
{
os << "counter = " << stats.counter << '\n';
os << "even = " << stats.even << '\n';
os << "odd = " << stats.odd << '\n';
os << "max = " << stats.max << '\n';
os << "total = " << stats.total << '\n';
return os;
}
int main(void) {
Number number(std::cout, std::cin);
Stats stats(number.number);
while (number.number != 0) {
stats.run(number.number);
number.ask_and_replace(std::cout, std::cin);
}
std::cout << stats;
return 0;
}