用Java写一个关于文字方面的循环语句
以下是一个使用Java编写的文字方面的循环语句示例:
public class TextLoop {
public static void main(String[] args) {
String text = "Hello, world!";
// 使用for循环打印文本10次
for (int i = 0; i < 10; i++) {
System.out.println(text);
}
System.out.println();
// 使用while循环打印文本5次
int count = 0;
while (count < 5) {
System.out.println(text);
count++;
}
System.out.println();
// 使用do-while循环打印文本3次
int num = 0;
do {
System.out.println(text);
num++;
} while (num < 3);
}
}
这个示例程序定义了一个名为TextLoop的类,其中包含了一个main方法。在main方法中,首先定义了一个字符串变量text,并将其初始化为"Hello, world!"。
然后使用for循环打印文本10次,循环变量i从0开始,每次循环递增1,直到i小于10为止。在循环体中,使用System.out.println方法打印文本。
接着使用while循环打印文本5次,定义一个整型变量count,初始值为0。在循环条件中判断count是否小于5,如果满足则执行循环体内的代码,即打印文本并将count递增1。
最后使用do-while循环打印文本3次,定义一个整型变量num,初始值为0。循环体内先执行一次打印文本的操作,然后判断num是否小于3,如果满足则继续执行循环体内的代码,即再次打印文本并将num递增1
原文地址: http://www.cveoy.top/t/topic/iSRi 著作权归作者所有。请勿转载和采集!