一 、判断某一年是否为闰年
在该类的主方法中接收用户输入的一个整数年份,然后通过闰年计算公式,判断这个年份是否为闰年,并在控制台输出判断结果。
代码如下:
public class Demo {
public static void main(String[] args) { // 主方法
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个年份:"); // 向控制台输出一个提示信息
long year;
year = sc.nextInt();
if (year % 4 ==0 && year % 100 != 0 || year % 400 == 0){
System.out.println(year+"是闰年!");
}else {
System.out.println(year+"不是闰年!");
}
}
}
二 、验证登录信息的合法性
在该类的主方法中接收用户输入的登录用户名与登录密码,然后通过if条件语句分别判断用户名与密码,并输出登录结果。
代码如下:
public class CheckLogin {
public static void main(String[] args) {
while (true){ // while循环 实现输入错误重新输入
Scanner sc = new Scanner(System.in); // 创建输入类的对象sc 从键盘中获取输入信息
System.out.print("请输入登录的用户名:"); // 接收用户输入的登录名
String username = sc.nextLine();
System.out.print("请输入登录密码:"); // 接收用户输入的登录密码
String password = sc.nextLine();
if (!username.equals("高晓松")){
System.out.println("输入的用户名错误!");
}else if (!password.equals("462580")){
System.out.println("输入的登录密码错误!");
}else { // 通过以上两个条件则默认通过登录验证 break退出程序
System.out.println("登录信息通过验证!");
break;
}
}
}
}