[英]Java for loop won't run…?
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.
我的老师告诉我,如果我使用while循环获取一个反控制变量,她会取下积分,所以我很想把它弄好。
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!
我尝试使用while循环,它完美地工作!
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.
EDIT part deux:感谢大家帮忙解决我的误解。 Onward to method documentation!
继续方法文档!
for ( int check = 0; check == value; check++ )
This will only run if check == value
. 这只会在
check == value
运行。 Modify to: 修改为:
for ( int check = 0; check < value; check++ )
尝试for ( int check = 0; check <= value; check++ )
而不是for ( int check = 0; check == value; check++ )
From the Oracle website (my emphasis): 来自Oracle网站 (我的重点):
The for statement provides a compact way to iterate over a range of values.
for语句提供了一种迭代一系列值的简洁方法。 Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied.
程序员经常将其称为“for循环”,因为它反复循环直到满足特定条件的方式。 The general form of the for statement can be expressed as follows:
for语句的一般形式可表示如下:
for (initialization; termination; increment) {
statement(s)
}
When using this version of the for statement, keep in mind that:
使用此版本的for语句时,请记住:
The initialization expression initializes the loop;
初始化表达式初始化循环; it's executed once, as the loop begins.
循环开始时,它执行一次。
When the termination expression evaluates to false, the loop terminates.
当终止表达式求值为false时,循环终止。
The increment expression is invoked after each iteration through the loop;
每次迭代循环后调用increment表达式; it is perfectly acceptable for this expression to increment or decrement a value.
这个表达式增加或减少一个值是完全可以接受的。
To get the same effect as: 获得与以下相同的效果:
int check = 0;
while (check < value) {
// something
}
your for
should look like this: 你
for
应该是这样的:
for (int check = 0; check < value; check++) {
// something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.