C++ 查找用户输入的一组数字中的最大数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27937137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the highest number in a set of numbers the user enters
提问by Glorpy
I bought the textbook C++ How to program 9th editionand I have come across a question that I am just stumped on, even though it is probably pretty simple. The question all summed up is this: "Use a while statement to determine and print the largest number of 10 numbers entered by the user". But here is the part that stumps me. The question wants me to use only 3 variables. counter, number, and largest. I know to make counter variable go up by 1 for each number entered until it gets to 10, and I know the number variable is used for input. I just can't seem to find out how to use the largest variable, or how to check to see what value is the largest without using other variables. This is all I have so far. Right now I put a break in the code so it wouldn't be an infinite loop.
我买了教科书C++ How to program 9th edition,我遇到了一个让我很难过的问题,尽管它可能很简单。总结起来的问题是:“使用while语句确定并打印用户输入的10个数字中的最大数字”。但这里是难倒我的部分。问题要我只使用 3 个变量。计数器、数量和最大。我知道让计数器变量为每个输入的数字增加 1,直到它达到 10,并且我知道数字变量用于输入。我似乎无法找出如何使用最大的变量,或者如何在不使用其他变量的情况下检查最大的值。这就是我目前所拥有的。现在我在代码中放了一个中断,所以它不会是一个无限循环。
UPDATED CODE
更新代码
#include <iostream>
using namespace std;
void main()
{
int counter = 0;
int number = 0;
int largest = 0;
cout << "Please enter up to 10 numbers and I will print the largest one on the screen.\n\n";
while (counter <= 10)
{
cout << "Number: ";
cin >> number;
counter++;
if (number > largest)
{
largest = number;
}
else if (counter == 10)
{
cout << "The largest number was: " << number;
break;
}
}
}
回答by Chris Maes
cout << "The largest number was: " << number;
should be
应该
cout << "The largest number was: " << largest;
回答by Peter Carnesciali
Inside the while loop, if number is greater than largest, then set largest = number.
在while循环内,如果数字大于最大值,则设置最大值=数字。
Then at the end you can output largest.
然后最后你可以输出最大的。
回答by Irrational Person
Solution(you don't even need a while loop)
解决方案(您甚至不需要 while 循环)
#define MAX_NUM 8
//user input goes in myints
int myints[MAX_NUM] = {3,7,2,5,6,4,9};
// using default comparison:
std::cout << "The largest element is " << *std::max_element(myints,myints+MAX_NUM) << '\n';
Other Solution using int arrays even though you can replace int array with one variable
使用 int 数组的其他解决方案,即使您可以用一个变量替换 int 数组
int main()
{
int largest = 0;
int myints[10] = {3,7,2,5,6,4,9,7,2,6};
for(int i =0 ; i< 10;i++)
{
if (myints[i] > largest)
{
largest = myints[i];
}
}
cout << largest << endl;
return 0;
}
回答by randPow
You just need to add in while loop checking is the number you entered bigger than largest. If it is you just store it in largest. And actually you are entering 11 numbers because you count from 0 to 10. Just set counter to 1 or while(counter < 10)
您只需要添加 while 循环检查是您输入的数字大于最大值。如果是,您只需将其存储在最大的位置。实际上,您输入了 11 个数字,因为您是从 0 数到 10。只需将 counter 设置为 1 或 while(counter < 10)
int counter = 1;
int number = 0;
int largest = 0;
cout << "Please enter up to 10 numbers and I will print the largest one on the screen.\n\n";
while (counter <= 10)
{
cout << "Number: ";
cin >> number;
counter++;
if (largest < number)
{
largest = number;
}
}
cout << largest << endl;