I am working on a homework assignment, and the crucial loop of the program is giving me trouble. My teacher has informed me that she will take off points if I use a while loop for a counter-controlled variable, so I'm anxious to get this right.
Here's what I would like to work, and what I feel in my heart should work:
for ( int check = 0; check == value; check++ ) {
int octal = getOctal();
int decimal = convertOctal( octal );
System.out.printf( "%d:%d", octal, decimal );
}
However, this loop does not run. I tried doing it with a while loop, and it worked perfectly!
int check = 0;
while ( check < value )
{
int octal = getOctal();
int decimal = convertOctal( octal );
System.out.printf( "%d:%d", octal, decimal );
check++;
}
Here is the rest of the main method:
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++;
}
}
}
Yes, this is an octal-to-decimal converter. I wrote the converter method myself from scratch and am ridiculously proud of it.
EDIT: MY QUESTION is, what's wrong here? EDIT part deux: Thanks all for your help in clearing up my misunderstanding. Onward to method documentation!
for ( int check = 0; check == value; check++ )
This will only run if check == value. Modify to:
for ( int check = 0; check < value; check++ )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With