如何编写一个不包含21的数字的f语句 [英] How do I write a f statement that will exclude numbers less than 21

查看:66
本文介绍了如何编写一个不包含21的数字的f语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

int main() {
   const int NUM_ELEMENTS = 8; // Number of elements
   int userVals[NUM_ELEMENTS]; // User numbers
   int i = 0;                  // Loop index
   
   
   // Prompt user to populate array
   cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl;

   for (i = 0; i <= NUM_ELEMENTS; ++i) {
      cout << "Value: " << endl;
      cin >> userVals[i];
   }
   
   // Determine values greater than 21
   for (i = 0; i <= NUM_ELEMENTS; ++i) {
      if   //This is where I am stuck.....
        cout << endl << userVals[i] << endl;
   }
   
   return 0;
}

推荐答案

您将输入的输入提供给数组中的每个位置,第一个用于。这意味着 userVals [i] 是您保存信息的地方。

要知道输入的数字是否大于21,你有比较它们(逻辑上),你知道它们的保存位置。不是吗?最后,阵列的位置可以被视为你在那一刻要求的变量。



我的意思是,在比较的概念中没有区别with



You are giving the entered inputs to every place in the array with the first for. Which means userVals[i] is the place where you are saving the info.
To know if the entered numbers are bigger than 21, you have to compare them (logically) and you know where they are saved. Don't you? At the end, a position of the array can be considered as the variable you are asking in that moment.

I mean, in the concept of comparison there is no difference with

int a = 1;
if (a > 0)

//  or

int a = 1
int b = 2
if (a > b)





结论...





in conclusion...

for (i = 0; i <= NUM_ELEMENTS; ++i) {
   if (userVals[i] > 21)  // or if (userVals[i] >= 22)
     cout << endl << userVals[i] << endl;
}


这篇关于如何编写一个不包含21的数字的f语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆