循环语句、嵌套循环、方法定义

循环
for循环

//for练习
public class Test_for {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			System.out.println(i);
		}
	}
}

while循环

public class Test_while {
	public static void main(String[] args) {
		int i=1;
		while(i<5) {
			System.out.println(i);
			i++;
		}
	}
}

综合练习
模拟用户登录

正确的用户名是123,密码是987

提示用户录入用户名和密码,进行判断,如果录入正确就提示“登录成功”;如果录入错误就提示“登录失败,请重新录入”

如果登录失败,就继续录入,直到登录成功为止

public class Test_login {
	public static void main(String[] args) {
		int user=123;
		int pass=987;
		Scanner sc = new Scanner(System.in);
		while(true) {
			System.out.println("输入用户名");
			int user1=sc.nextInt();
			System.out.println("输入密码");
			int pass1=sc.nextInt();
			if (user1==123&&pass1==987) {
				System.out.println("账号密码正确");
				break;
			}else {
				System.out.println("输入不正确请重新输入");
			}
		}
	}
}

循环嵌套
输出99乘法表

public class Test_99 {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j+"*"+i+"="+i*j+" ");
			}
			System.out.println();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值