我有使用循环的问题 [英] I have problem using loop

查看:92
本文介绍了我有使用循环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Write a program that prompts an  integer, and write multiplication table from 1 to that number. For example if the integer is 3 the output should be
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 16 18 20
3 6 9 12 15 18 21 24 27 30

Use nested for loops (two for loops one outer loop, one inner loop).





< b>我尝试了什么:





What I have tried:

#include <iostream>

using namespace std;

int main()
{
    int num ,b,c,d;

  cout <<"Enter an  number to get the  multiplication table from 1 to that number"<<endl;
cin>>num;
for (b=1;b<=num;b++)
{ for(c=1;c<=num;c++)
{ d=b*c;
cout<<d;}
cout<<endl;}

  return 0;
}

推荐答案

与您的样本相比,您在一个轴上从1到10以及从1到1运行 n 另一方面,你在代码的两个轴上从1运行到 n ...



将内循环更改为最多10个,并且为了便于阅读,在打印每个数字后添加一个标签...



In contrast to your sample, where you are run from 1 to 10 on one axis and from 1 to n on the other, you run from 1 to n on both axis in your code...

Change the inner loop to run up to 10, and for the readability add a tab after each number printed...

#include <iostream>

using namespace std;
 
int main()
{
    int num, b, c, d;
 
    cout << "Enter an  number to get the  multiplication table from 1 to that number" << endl;
    cin >> num;
    
    for (b = 1; b <= num; b++)
    { 
        for(c = 1; c <= 10; c++)
        {
            d = b * c;

            cout << d << "\t";
        }
        cout << endl;
    }

    return 0;
}</iostream>


使用调试器,允许您查看代码执行情况,它将帮助您了解错误。

在这个例子中,每行停在10,1你的循环也必须在10停止。



你应该学习使用调试器尽快。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Use the debugger, by allowing you to see your code executing, it will help you to understand what is wrong.
In the example, each line stops at 10, 1 of your loops must stops at 10 too.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我有使用循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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