【发布时间】:2020-02-24 01:30:43
【问题描述】:
我应该编写将 0 和 10 之间的数字乘以 2 和 10 的代码。它应该使用 for 循环。我能够想出这个作为一个while循环,但是我尝试转换的任何东西都会给我带来一堆错误。我发布的 while 代码有效,它只需要采用 for 循环格式。
while(numberCounter <= 10)
{
byTen = numberCounter * 10;
byTwo = numberCounter * 2;
System.out.println(numberCounter + "\t" + byTwo + "\t" + byTen);
numberCounter++;
}
这是整个问题:
// NewMultiply.java - This program prints the numbers 0 through 10 along
// with these values multiplied by 2 and by 10.
// Input: None.
// Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10.
public class NewMultiply
{
public static void main(String args[])
{
String head1 = "Number: ";
String head2 = "Multiplied by 2: ";
String head3 = "Multiplied by 10: ";
int numberCounter = 0; // Numbers 0 through 10.
int byTen; // Stores the number multiplied by 10.
int byTwo; // Stores the number multiplied by 2.
final int NUM_LOOPS = 10; // Constant used to control loop.
// This is the work done in the housekeeping() method
System.out.println("0 through 10 multiplied by 2 and by 10" + "\n");
// This is the work done in the detailLoop() method
// Write for loop
// This is the work done in the endOfJob() method
System.exit(0);
} // End of main() method.
} // End of NewMultiply class.
我想通了。非常感谢大家的帮助。问题是我有 (numberCounter
【问题讨论】:
-
欢迎来到 SO。您是否查看过有关
for循环的文档或教程?如果你有答案,对你来说是很明显的。从docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html开始 -
语法是for(numberCounter = 1; numberCounter
标签: java for-loop while-loop