java-for循环问题
作者:小心!! 时间:2022-01-22 12:46:53
for循环
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。
for循环执行的次数是在执行前就确定的。
语法格式如下:
for(初始化;布尔表达式;更新){
//代码语句
}
练习
public class Demo07 {
public static void main(String[] args) {
//计算0-100之间的奇数和,偶数和
int o=0;
int j=0;
for (int i = 0; i <=100; i++) {
if(i%2==0)
{
o=o+i;
}
else{
j=j+i;
}
}
System.out.println("偶数和为:"+o);
System.out.println("奇数和为:"+j);
}
}
public class Demo077 {
public static void main(String[] args) {
//用for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i <=1000; i++) {
if(i%5==0)
{
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.println();
// System.out.print("\n");
}
//printIn 输出完全换行
//print输出完,不会换行
}
}
}
public class Demo0777 {
public static void main(String[] args) {
/*打印九九乘法表
1*1= 1
2*1= 2 2*2= 4
3*1= 3 3*2= 6 3*3= 9
4*1= 4 4*2= 8 4*3=12 4*4=16
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */
//先打印第一列
//将固定的1用循环包起来
//去掉重复项,i<=j
//调整样式
for(int j=1;j<=9;j++){
for(int i=1;i<=j;i++){
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
public class Demo0777 {
public static void main(String[] args) {
/*打印九九乘法表
1*1= 1
2*1= 2 2*2= 4
3*1= 3 3*2= 6 3*3= 9
4*1= 4 4*2= 8 4*3=12 4*4=16
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */
//先打印第一列
//将固定的1用循环包起来
//去掉重复项,i<=j
//调整样式
int i=1;
int j=1;
while(j<=9){
i=1;
while(i<=j)
{
System.out.print(j+"*"+i+"="+(j*i)+"\t");
i++;
}
System.out.println();
j++;
}
}
}
增强for循环
Java5引入了一种主要用于数组或者集合的增强for循环。
java增强for循环的语法格式如下:
for(声明语句:表达式){
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class Demo07f {
public static void main(String[] args) {
int []a={10,20,30,40}; //定义了一个数组
//遍历数组的元素
for(int i=0;i<4;i++)
{
System.out.println(a[i]);
}
System.out.println("------------");
for(int j:a){
System.out.println(j);
}
}
}
来源:https://blog.csdn.net/qq_57935389/article/details/123479093
标签:java,for,循环
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何让java只根据数据库表名自动生成实体类
2022-02-24 04:25:52
![](https://img.aspxhome.com/file/2023/2/61322_0s.png)
mybatisplus逻辑删除基本实现和坑点解决
2021-05-24 11:35:50
![](https://img.aspxhome.com/file/2023/2/78032_0s.png)
SpringBoot+JSON+AJAX+ECharts+Fiddler实现前后端分离开发可视化
2021-11-12 14:49:17
![](https://img.aspxhome.com/file/2023/2/61562_0s.png)
Springboot整合Freemarker的实现详细过程
2023-01-23 22:09:57
![](https://img.aspxhome.com/file/2023/4/84164_0s.jpg)
Netty与NIO超详细讲解
2022-08-16 00:44:37
![](https://img.aspxhome.com/file/2023/2/65362_0s.png)
Java中lambda表达式实现aop切面功能
2022-12-02 09:44:59
![](https://img.aspxhome.com/file/2023/5/84625_0s.png)
SpringCloud消息总线Bus配置中心实现过程解析
2023-02-14 10:05:18
![](https://img.aspxhome.com/file/2023/7/64427_0s.png)
Java类和对象的设计原理
2023-10-06 07:00:32
Flutter 图片开发核心技能快速掌握教程
2023-08-16 22:01:51
![](https://img.aspxhome.com/file/2023/4/108294_0s.png)
Java中用enum结合testng实现数据驱动的方法示例
2021-08-04 07:44:21
![](https://img.aspxhome.com/file/2023/9/65179_0s.png)
Java 超详细讲解IO操作字节流与字符流
2022-04-28 20:09:57
![](https://img.aspxhome.com/file/2023/3/74973_0s.png)
关于后端如何解决跨域的问题说明
2023-09-19 00:59:10
![](https://img.aspxhome.com/file/2023/7/79967_0s.jpg)
使用Feign传递请求头信息(Finchley版本)
2023-06-07 22:38:07
![](https://img.aspxhome.com/file/2023/2/62742_0s.jpg)
详解Jvm中时区设置方式
2023-12-09 02:59:19
Java流程控制语句之If选择结构
2023-11-11 04:02:29
![](https://img.aspxhome.com/file/2023/3/58853_0s.png)
老生常谈Java异常处理和设计(推荐)
2023-08-21 02:26:06
![](https://img.aspxhome.com/file/2023/8/71638_0s.jpg)
SpringBoot防止大量请求攻击的实现
2023-11-24 16:42:54
![](https://img.aspxhome.com/file/2023/1/59731_0s.jpg)
java 交换两个数据的方法实例详解
2021-12-06 00:56:04
java模拟http的Get/Post请求,并设置ip与port代理的方法
2021-11-25 11:30:57
java中的HashMap多层嵌套
2023-11-27 07:34:52
![](https://img.aspxhome.com/file/2023/4/60344_0s.jpg)