当前位置:网站首页>Day009循环结构(练习)
Day009循环结构(练习)
2022-07-21 02:08:00 【陌 年】
求100以内的所有素数
(素数:一个大于1的自然数,除了1和它本身外,不能被其他自然数整除)
package cn.bdqn.demo03;
public class Practice {
public static void main(String[] args) {
/*
* 求100以内的所有素数(素数:一个大于1的自然数,除了1和它本身外,不能被其他自然数整除)
*/
// 初始值
int i = 2;
while (i <= 100) {
boolean flag = true;
for(int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(i);
}
i++;
}
}
}
写一个方法,此方法实现输出100-999之间的水仙花数。
package cn.bdqn.demo03;
public class Practice02 {
public static void main(String[] args) {
// 写一个方法,此方法实现输出100-999之间的水仙花数。
int i= 100;
while(i<=999){
int a = i%10;
int b = i/10%10;
int c = i/100%10;
if(a*a*a+b*b*b+c*c*c==i){
System.out.println(i);
}
i++;
}
}
}
求出1-1/2+1/3-1/4…..1/100的和
package cn.bdqn.demo03;
public class Practice05 {
public static void main(String[] args) {
// 求出1-1/2+1/3-1/4…..-1/100的和
int i = 1;
double sum = 0;
while (i <= 100) {
double a = -(1.0 / i);// i作为分母
if (i % 2 == 0) {
a = -(1.0 / i);
} else {
a = 1.0 / i;
}
sum += a;
i++;
}
System.out.println(sum);
}
}
输出九九乘法表
package cn.bdqn.demo03;
public class Practice11 {
public static void main(String[] args) {
// 输出九九乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (j <= i) { //当i=1时,j=1,满足条件,此时执行输出,执行完毕后j自加,(j=2)>(i=1),不满足条件,跳出;
//此时i自加,i=2,再进入j的for循环,此时1<2满足条件,执行输出1*2=2,执行完毕后j自加,
//j=2,满足j<=i,执行输出2*2=4,执行完毕后j自加,j=3,此时j<=i不成立,跳出循环;依次类推
System.out.print(j + "*" + i + "=" + (j * i) + " ");
//j==i时换行,实现乘法表的格式
if (j == i) {
System.out.println(" ");
}
}
}
}
}
}
边栏推荐
- 通用分頁(分頁代碼的封裝)
- JSON value acquisition and traversal [json]
- 【IoT毕设.3】STM32单片机+机智云AIoT+猪舍监测与系统硬件设计
- 通用分页(分页代码的封装)
- NepCTF2022
- High performance server model summary
- JSON概述
- Getting started with ctfshow web (PHP deserialization)
- Mil-101-fe @uio-66-nh2 metal organic complexes (MOFs) composites | poly (1-h benzoindole) /zn mof/wo3 ternary Nanocomposites
- 【刷题笔记】阶乘尾数
猜你喜欢
2022 DSCTF首届数字空间安全攻防大赛
【翻译】在Kubernetes上设计和部署可扩展应用程序的原则
c语言入门---操作符
Authoring practice | authorization management makes it easier for enterprise users to log in
A little learning of JNDI injection
ctfshow web入门(phpcve)
Solve the problem that the time field is blank when kettle8.2 version table input excel output
通用分頁(分頁代碼的封裝)
The domestic epidemic is repeated. How can offline physical stores be transformed to break through the dilemma?
Table auto paging
随机推荐
洛谷P5142 区间方差 题解
Polyethyleneimine (PEI) modified mil-101 (CR) | polyacid based metal organic framework material (pom-mof) | ferrocene modified mil-88b | Qiyue biology
ctfshow web入门(SSRF)
基于JSP/SERVLET实现的人脸识别考勤系统
How much has changed from new retail to community group purchase?
与时俱进的不止是产品 从卡罗拉锐放看一汽丰田“二次创业”的信心
Leetcode 201 Digit range bitwise and (2022.07.20)
JNDI注入之略微学学
5-carboxyl tetramethyl rhodamine labeled peptide nucleic acid PNA | TMR ahx- (Tamra ahx-) -pna | fluorescein labeled peptide nucleic acid mechanism
Demystifying Closures, Futures and async-await in Rust–Part 3: Async & Await
Getting started with ctfshow web (file upload)
Review the knowledge points of thinking and practice - "Ideological and moral basis and legal cultivation"
【翻译】用Kubernetes + Helm + Flux提高稳定性和可靠性!
丹磺酰荧光素标记肽核酸偶联多肽|Dansyl-Ahx-PNA荧光素标记肽核酸的合成路线
一种非极大值抑制(non_max_suppression, nms)的代码实现方式
FCRP-D---帆软官网模拟题,kettle模块
解决Kettle8.2版本錶輸入-Excel輸出時,時間字段空白
Nh2-mil-125 (TI) /tio2 composite nanofibers | lanthanide metal organic framework( [email protected] )|MOF organic skeleto
Getting started with ctfshow web (code audit)
无码时代,企业数字化转型该如何发展?