java 写出while语句的句法结构写一个关于while的练习要求 写出代码注释
while语句的句法结构:
while (condition) { // code to be executed while the condition is true }
练习:编写一个程序,使用while循环输出1到10之间的所有奇数。
// define a variable to store the current number int num = 1;
// use a while loop to iterate through numbers 1 to 10 while (num <= 10) {
// check if the number is odd
if (num % 2 != 0) {
System.out.println(num); // print the odd number
}
// increment the number
num++;
}
// output: 1 3 5 7 9
原文地址: https://www.cveoy.top/t/topic/cHVh 著作权归作者所有。请勿转载和采集!