【问题标题】:Write a program that asks the user for up to 10 integers (it could be fewer); using arrays, and for loops编写一个程序,要求用户输入最多 10 个整数(可能更少);使用数组和 for 循环
【发布时间】:2017-04-15 18:43:04
【问题描述】:

编写一个程序,要求用户输入最多 10 个整数(可能更少);当用户输入零时停止提示。然后以相反的顺序列出数字。使用以下示例运行作为指导:

    Enter a number (0 to stop): 11
    Enter a number (0 to stop): 33
    Enter a number (0 to stop): 55
    Enter a number (0 to stop): 77
    Enter a number (0 to stop): 99
    Enter a number (0 to stop): 0
    Your numbers in reverse order are: 
    99, 77, 55, 33, 11

以下是我当前的代码,似乎无法弄清楚我做错了什么。

#include <iostream>
#include <string>
using namespace std;


int main( ) {
int max = 10;
int num = 1;
int userVal[max];
int i = 0;


while(num <= max) {
  cout << "Enter a number (0 to stop): ";
  cin >> userVal[i];
  cout << userVal[i] << endl;
  if(userVal[i] == 0) {
     break;
  }
  ++num;
}
cout << num << endl;
cout << "Your numbers in reverse order are: " << endl;
for(i = num; i >= 0; --i) {
  cout << userVal[i];
  if(i < num - 2) {
     cout << ", ";
  }
}

return 0;
}

下面是我得到的输出,如上所述,我想反向打印数字

    Enter a number (0 to stop): 11
    Enter a number (0 to stop): 33
    Enter a number (0 to stop): 55
    Enter a number (0 to stop): 77
    Enter a number (0 to stop): 99
    Enter a number (0 to stop): 0
    6
    Your numbers in reverse order are: 
    6553532600-6638791760, 4197268, 0, 0, 

【问题讨论】:

  • 你永远不会在你的第一个while 中增加i,你也可以只增加num - 1
  • 旁注,不要使用using namespace std;
  • for 循环也进行了一次迭代。

标签: c++ arrays loops for-loop


【解决方案1】:

首先,在您的 while 循环中,您使用 i 来分配数组中的值,您需要 num 代替。其次,应将 num 设置为零,以便填充数组的第一个元素。试试这个代码:

#include <iostream>
#include <string>
using namespace std;


int main( ) {
int max = 10;
int num = 0;
int userVal[max];
int i = 0;


while(num <= max) {
   cout << "Enter a number (0 to stop): ";
   int number;
   cin >> number;

   cout << number << endl;
   if(number == 0) {
      break;
   }

    userVal[num] = number;
    ++num;

}
cout << num << endl;
cout << "Your numbers in reverse order are: " << endl;
for(i = num; i >= 0; --i) {
   cout << userVal[i];
   if(i < num - 2) {
       cout << ", ";
  }
}

return 0;
}

【讨论】:

  • breakif 中时不必使用else
【解决方案2】:

首先:您使用了两个不同的变量 - numi - 但您只增加了 num 变量。更好:

int num = 0; // start with 0 - this is the number read initially: nothing...
while(num < max) // need to use < instead
    std::cin >> userVal[num]; // can use your counter now as index, too...

但是,您必须相应地调整输出循环:

for(int i = num - 1; i >= 0; --i)

第二:您希望始终打印逗号 - 除非最后一个索引 (0);所以:

if(i > 0)
    std::cout << ", ";

最后:你应该声明 max const 或 constexpr。 C++ 不支持堆栈上的可变长度数组。通常,您的编译器应该抱怨int userVal[max];(也许这次它会接受它,因为它认为 max 永远不会改变 - 但这不是 C++ 标准)。

【讨论】:

    【解决方案3】:

    感谢大家的帮助,下面的代码是我想出的,它结合了你的每一个建议。

    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main( ) {
      int max = 10;
      int num = 1;
      int userVal[max];
      int i = 0;
    
    
      while(num < max) {
       cout << "Enter a number (0 to stop): ";
       int number;
       cin >> number;
       cout << number << endl;
    
       if(number == 0) {
         break;
       }
       userVal[num] = number;
       ++num;
      }
      cout << "Your numbers in reverse order are: " << endl;
      for(i = num - 1; i > 0; --i) {
        cout << userVal[i];
        if(i > 1) {
         cout << ", ";
        }
      }
      cout << endl;
    
      return 0;
     }
    

    我收到以下输出

        Enter a number (0 to stop): 11
        Enter a number (0 to stop): 33
        Enter a number (0 to stop): 55
        Enter a number (0 to stop): 77
        Enter a number (0 to stop): 99
        Enter a number (0 to stop): 0
        Your numbers in reverse order are: 
        99, 77, 55, 33, 11
    

    【讨论】:

      【解决方案4】:

      检查输入循环中的索引。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-17
        • 2016-11-03
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多