【问题标题】:Nested For - Loops to create multiplication table C++嵌套 For - 循环创建乘法表 C++
【发布时间】:2017-03-26 16:49:37
【问题描述】:

我已经尝试克服这个问题几个小时了,我似乎有一种方法来解决这种情况。似乎选择语句的使用在创建必要的表时起作用。虽然存在格式问题。

我想知道是否有办法创建同一张表 只使用我们教授提到的嵌套 for 循环

选择语句是否必要,或者我们可以实现一个嵌套的 for 循环系统来获得相同的结果?

下图为必填表:


但下面的图片是我所拥有的

下面是我的代码:

for (int i = 0; i <= numChoice; ++i)
    {
        if (i == 0)
        {
            for (int k = 1; k <= numChoice; ++k)
            {
                cout << "    " << k;
            }
            cout << "\n";
        }
        else
        {
            cout << i << " | ";
            for (int j = 1; j <= numChoice; ++j)
            {
                if (j*i <= 9)
                {
                    cout << "   " << j*i << "|";
                }
                else if (j*i > 9 && j*i <= 100)
                {
                    cout << "  " << j*i << "|";
                }
                else if (j*i > 99 && j*i <= 999)
                {
                    cout << " " << j*i << "|";
                }
            }
            cout << "\n";
            for (int k = 0; k <= numChoice; ++k)
            {
                if (k == 0)
                {
                    cout << "-|";
                }
                else
                {
                    cout << "----|";
                }
            }
            cout << "\n";
        }
    }

【问题讨论】:

  • 你的问题不是很清楚。是关于表格的格式吗?您的意思是您用来知道位数是多少的“选择语句”?可以让您格式化表格的 io 操纵器:rightsetw
  • 您的代码示例中已经 嵌套了 for 循环,所以我不明白这个问题。
  • @zett42 主要问题是 - 我是否需要选择语句来完成此任务,或者是否可以只使用嵌套的 for 循环来获得相同的结果。
  • @tobi303 - 是的,我知道 setw 功能。我主要关心的是我上面提到的选择语句是否必要。
  • @FranticCode 您不需要使用选择语句;在下面查看我的答案。

标签: c++ for-loop nested


【解决方案1】:

以下代码使用 no if else 构造。格式可以通过setw得到,用于设置整数的宽度。下面的代码产生完美的输出。

   #include<iostream>
   #include<iomanip>
   using namespace std;
   int main()
   { 
   int i,j;    
   cout<<"     "<<1;//5 space chars

   for(i = 2;i <= 10;++i)
       cout<<"    "<<i;//4 space chars

   cout<<endl;
   cout<<"   ----|";

   for(i = 2;i <= 10;++i)
       cout<<"----|";

   cout<<endl;

   for(i = 1;i <= 10;++i)
   {
       cout<<setw(2)<<i<<"|";

       for(j = 1;j <= 10;++j)
          cout<<setw(4)<<j*i<<"|";

       cout<<endl;
       cout<<" -|----";

       for(j = 2;j <= 9;++j)
          cout<<"|----";

       cout<<"|----|";
       cout<<endl;

   }
   return 0;
   }

【讨论】:

  • 嘿,谢谢,这帮助了很多!唯一的问题是它不会超过 10 我现在正在重写它但是行的格式被抛弃了但是还是谢谢你1
【解决方案2】:

@FranticCode。我也和你在同一个班,这个家庭作业也有问题。我仍然不明白,但我想出了如何操作 Sumeet 的代码来给我们正确的格式。我现在唯一遇到的问题是在第一个乘法表之后和菜单重新显示之前添加一个空格。我会分享我所拥有的,也许你可以弄清楚。还是要请教授复习第 5 章,因为我想学习而不是只提交作业。

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    char userSelection;
    int numForTable;
    int col;
    int row;

do
{
    cout << "MENU" << endl
         << "a) Generate Multiplication Table" << endl
         << "q) Quit the program" << endl
         << "Please make a selection: ";
    cin  >> userSelection;

    if (userSelection == 'a')
    {
        cout << "Please enter a number for your multiplication table: " << endl;
        cin >> numForTable;

        while (numForTable < 1 || numForTable > 10)
        {
            cout << "Please enter a number between 1 & 10." << endl;
            cin >> numForTable;
        }

        cout << "\n"
             << "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
             << "\n"
             << "    " << 1;

        for (col = 2; col <= numForTable; ++col)

            cout << "    " << col;
            cout << endl;
            cout << "   ----|";


        for (col = 2; col <= numForTable; ++col)

            cout << "----|";
            cout << endl;


        for (col = 1; col <= numForTable; ++col)
             {
                 cout << setw(2) << col << "|";

                 for (row = 1; row <= numForTable; ++row)

                     cout << setw(4) << col * row << "|";
                     cout << endl;
                     cout << " -|----";

                 for (row = 2; row <= numForTable - 1; ++row)

                     cout << "|----";
                     cout << "|----|";
                     cout << endl;       
             }
    }

     else if (userSelection != 'q')
     {
         cout << "Invalid Selection\n" << endl;
     }

     else if (userSelection == 'q')
     {
         cout << " You have chosen to quit the program. Thank you for using!" << endl;
     }
}

while (userSelection != 'q');

//system("PAUSE");
return 0;
}

【讨论】:

  • 嘿! shroyer 是你的教授吗?你在哪个课时?
  • 她是 :) 和周一晚上的课程。我是凯文。我发布了相同的问题,或多或少,并被某人的评论引导到你的帖子,他说我们有非常相似的问题
【解决方案3】:

很好奇我是否可以像我声称的那样简单地添加行,这需要一些摆弄,但结果如下(更新下面的代码也有行)。

#include <iostream>
#include <iomanip>

using namespace std;

int main(){
    int counter;
    int counter2;
    int amount;
    
    cout << " |-----------------------------------------------------------|" << endl; // first line of table.
    
    for(counter=1;counter<11;counter++){            // the 2 for lines create our 2 dimensional table
        for(counter2=1;counter2<11;counter2++){
            
            cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>, 
            //setting minimum width to 3 for numbers.
            
        }
        
        cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
                
        cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
    }
    return 0;
}

【讨论】:

    【解决方案4】:

    使用以下构造:

    for (int i=0; i<=numChoice; i++) // display first row of numbers
     cout <<"\t" << i << "\t";
    cout << "\n";
    for (int i=0; i <=numChoice; i++) {
      cout << i << "\t";
      for (int j=0; j <=numChoice; j++)
        cout << i*j << "\t";
    cout << "\n";
    }
    

    【讨论】:

    • 我尝试使用您提供的代码,但打印的结果好坏参半。还是谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多