public class test5 {
public static void main(String[] args) {
//用于计数,循环的次数
int total=0;
while(true) {
//循环一次,计数器加一
total++;
//随机生成0-100的数
int i=(int)(Math.random()*100);
System.out.println("生成的数为:"+i);
//当随机数等于88时退出循环
if(i==88) {
break;
}
}
System.out.println("循环结束,循环次数为:"+total+" "+"times");
}
}
生成 0-100 随机数,直到生成 88 为止,停止循环.
最新推荐文章于 2024-10-16 10:43:50 发布
本文介绍了一个简单的Java程序示例,该程序使用无限循环生成0到100之间的随机整数,直到生成特定数值88时退出循环。文中展示了如何使用Math.random()方法结合循环结构来实现这一功能,并记录了循环的总次数。
1159

被折叠的 条评论
为什么被折叠?



