循环 + 方法

一、循环结构

1.1 格式:

while(布尔表达式){
     语句块;
}

1.2 break:让循环提前结束(结束所有循环)

1.3 continue:跳过这次循环,进入下次循环。

1.4 for循环:

for(表达式1;表达式2;表达式3){
    循环体;
}

其中表达式2为布尔表达式。

1.5 do while 循环:

do{
  语句体
}while(循环条件)

注:先执行循环语句,再判定条件,因此至少会执行一次。

二、输入、输出

2.1 从键盘输入: 

Scanner scan = new Scanner(System.in);
int a = scan.nextInt();    //输入整数
String str = scan.nextLine();     //输入字符串

注:若用nextLine,前面不能有int,否则nextLine部分不会输入。 

2.1.1 循环输入:

Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
     int n = scan.nextInt();
}

2.2 随机生成一个数:

2.2.1

Random random = new Random();
int randnum = random.nextInt(bound:100);
int randnum = random.nextInt(bound:100)+1;    //[1,101)

2.2.2 伪随机:

Random random = new Random(111);
​

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值