我有问题使用while和for循环 [英] I have problem using while and for loop

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

问题描述

问题是:

编写一个程序来计算前五个整数的平方和和平均值。

尝试写它也用于用户定义的数字而不是5.



总和= 1 * 1 + 2 * 2 + 3 * 3 + 4 * 4 + 5 * 5;



用for循环写一个程序,用while循环写一个程序。



我试过的:



  #include   <   iostream  >  

使用 命名空间标准;

int main()
{
int 数字,计数器;
float 平均值;
float sum = 0 ;
cout<< 输入五个数字以查找前五个整数的平方和<< ENDL;
cin>> number;
for (counter = 0 ; counter< 5; counter ++){
sum =总和+(编号*编号);
cin>> number;
(数字+数字);
}
cout<< 总和为:<< ; endl<< sum<< endl;
average = sum / counter;
cout<< 平均值为:<< endl<<平均;
return 0 ;
}

解决方案

while循环也很简单:



  int  counter =  0 ; 
while (counter< 5 ){
// 你的代码
counter ++;
}





对于可变长度的输入,您只需要另一个var。

< pre lang =c ++> int theCount = 0 ;
cout<< 输入计算数字的数量<< endl;
cin>> theCount;
int counter = 0 ;
while (counter< theCount){ // 用于退出循环
// 您的代码
counter ++;


你的代码看起来很奇怪,你应该在调试器下运行它,并在逐行步骤时检查变量。



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



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

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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


the question is :
Write a program that compute the sum and average of square of first five integers.
Try to write it also for user defined number instead of 5.

Sum = 1*1 + 2*2 + 3*3 + 4*4 + 5*5 ;

Write the programs one with for loop and one with while loop.

What I have tried:

#include <iostream>

using namespace std;

int main()
{
int number , counter ;
float average;
float sum =0;
cout<< "Enter five numbers to find the sum and the average of square of first five integer"<<endl;
cin >>number;
for (counter=0;counter<5;counter++){
    sum=sum+(number*number);
    cin>>number;
    (number+number);
}
cout<<"The sum is :"<<endl <<sum<<endl;
average =sum/counter;
cout<<"The average is :"<<endl<<average;
    return 0;
}

解决方案

The while loop is also easy:

int counter = 0;
while( counter < 5 ) {
// your code
counter ++;
}



For a variable length of input you only need a another var.

int theCount = 0;
cout<< "Enter count of numbers for computation"<<endl;
cin >> theCount;
int counter = 0;
while( counter < theCount ) { //use for exit loop
// your code
counter ++;


Your code is weird looking, you should run it under debugger and inspect the variables as you step line by line.

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.


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

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