感谢你们每一位提前给予的帮助。
问题一:
我今天正在写一个程序,它应该显示用户将输入的10个值中的最高值。然而,每当我测试程序和输入值时,它似乎显示了不止一个“最大值”。我想这可能与我过度使用cout和cin函数有关。程序似乎会沿着用户输入的值列表向下移动,并选择列表中最高的值。
例如:
2, 4, 5, 1, 7, 8, 3, 2, 9它将选择5、8和9作为最高值。
问题二:
我想知道是否有一种方法可以简化我在下面编写的代码(参考cout和cin函数)。例如,制作一个while函数,并具有如下函数:
while (something)
cout << "Please input the number of pancakes person " << x << "has ate.\n"
cin >> something // Either x or a pancakes function. I tried both.
count++ // Sorry, I would just assume that the count function would be used in this scenario.问题3:
关于我的程序中的最大值输出,会不会是我把数组搞乱了?
代码:
#include <iostream>
using namespace std;
int main()
{
int pancakes [10];
int x;
int largestValue = 0;
cout << "Please input the number of pancakes person 1 has ate.\n";
cin >> pancakes[0];
cout << "Please input the number of pancakes person 2 has ate.\n";
cin >> pancakes[1];
cout << "Please input the number of pancakes person 3 has ate.\n";
cin >> pancakes[2];
cout << "Please input the number of pancakes person 4 has ate.\n";
cin >> pancakes[3];
cout << "Please input the number of pancakes person 5 has ate.\n";
cin >> pancakes[4];
cout << "Please input the number of pancakes person 6 has ate.\n";
cin >> pancakes[5];
cout << "Please input the number of pancakes person 7 has ate.\n";
cin >> pancakes[6];
cout << "Please input the number of pancakes person 8 has ate.\n";
cin >> pancakes[7];
cout << "Please input the number of pancakes person 9 has ate.\n";
cin >> pancakes[8];
cout << "Please input the number of pancakes person 10 has ate.\n";
cin >> pancakes[9];
for (int x = 0; x < 10; x++)
if (pancakes[x] > largestValue)
{
largestValue = pancakes[x];
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}
return 0;
}发布于 2015-11-09 18:15:33
问题是,每次在这段代码中选择一个新的max-value时都会打印出来:
for (int x = 0; x < 10; x++) {
if (pancakes[x] > largestValue) {
largestValue = pancakes[x];
}
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}相反,它应该是:
for (int x = 0; x < 10; x++) {
if (pancakes[x] > largestValue) {
largestValue = pancakes[x];
}
}
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";顺便说一下:我想你要么用“吃”(不用“有”),要么用“吃过了”。
关于没有硬编码人员数量的另一个问题:首先,我将使用std::vector而不是数组。然后你会问,有多少人。现在,您可以创建一个具有如此多次迭代的for-循环。在每次迭代中,用户都会被问到person X吃了多少煎饼,然后使用std::vector::push_back将这个数字附加到向量的末尾。最后,你,你只需要像你已经做的那样选择最大的数字。只要让for循环的上限为< vector.size()即可。
发布于 2015-11-09 18:17:06
for (int x = 0; x < 10; x++)
if (pancakes[x] > largestValue)
{
largestValue = pancakes[x];
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}一旦找到比以前看到的更大的结果,您就会立即显示结果。您应该等到检查完所有的值。
for (int x = 0; x < 10; x++)
if (pancakes[x] > largestValue)
{
largestValue = pancakes[x];
}
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";发布于 2015-11-09 18:16:09
cout语句应该在循环之外。每次输入if语句时都会打印当前的cout (每次找到新的largestValue时)。
for (int x = 0; x < 10; x++) {
if (pancakes[x] > largestValue)
{
largestValue = pancakes[x];
}
}
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";https://stackoverflow.com/questions/33606721
复制相似问题