我正在做家庭作业,程序的关键循环给我带来了麻烦。我的老师告诉我,如果我对反控制变量使用 while 循环,她会扣分,所以我急于做对。
下面是我想做的,我心里觉得应该做的:
for ( int check = 0; check == value; check++ ) {
int octal = getOctal();
int decimal = convertOctal( octal );
System.out.printf( "%d:%d", octal, decimal );
}
但是,这个循环不会运行。我试着用一个 while 循环来完成它,它完美地工作了!
int check = 0;
while ( check < value )
{
int octal = getOctal();
int decimal = convertOctal( octal );
System.out.printf( "%d:%d", octal, decimal );
check++;
}
这是主要方法的其余部分:
public static void main ( String args[] )
{
int value = getCount();
while ( value < 0 )
{
System.out.print( "\nYou must enter a positive number" );
value = getCount();
}
if ( value == 0 )
{
System.out.print( "\n\nNo numbers to convert.\n\n" );
}
else
{
int check = 0;
while ( check < value )
{
int octal = getOctal();
int decimal = convertOctal( octal );
System.out.printf( "%d:%d", octal, decimal );
check++;
}
}
}
是的,这是一个八进制到十进制的转换器。我自己从头开始编写转换器方法,并为此感到无比自豪。
编辑:我的问题是,这里出了什么问题? 编辑两部分:感谢大家帮助消除我的误解。继续阅读方法文档!
请您参考如下方法:
for ( int check = 0; check == value; check++ )
这只会在 check == value
时运行。修改为:
for ( int check = 0; check < value; check++ )