盖伊的,我需要帮助...我得在控制台上画一个正弦波给我上大学课。但我有点纠结于如果..为什么第二个if语句不从第一个if/else语句中取新的“y_1”?也许有人能解释一下,我错在想^^多谢
#include <iostream>//Bibliothek fuer IO (Input-Output)
#include <cmath>
using namespace std;
int main()
{
int i,j=0;
double k,l=0.0;
int y_1=0;
double x,y_=0.0;
char a[80][24];
for(i=0; i<25; i++)
{
for(j=0; j<81; j++)
{
a[j][i]='.';
}
}
for(k=0; k<81; k++)
{
for(l=0; l<24; l++)
{
x=(k/80)*2*M_PI;
if(sin(x)>=0)
{
y_=12-(sin(x)*12);
int y_1=y_;
}
else
{
y_=12+(sin(x)*12);
int y_1=y_;
}
if(y_1==l)
{
int k=k;
int l=l;
a[k][l]='*';
}
}
}
for(i=0; i<25; i++)
{
for(j=0; j<81; j++)
{
cout<<a[j][i];
}
cout<<endl;
}
cout<<"Quit:Press Enter"<<endl;
cin.get();
return 0;
}
所有变量都有一个作用域。
// x_1 is not visible here, because x_1 is not introduced yet
{ // <-- this begins a scope
int x_1 = 123;
} // <-- this ends a scope
// that x_1, that was initialised by 123 is not visible here anymore