java实现简单的登录操作

java实现简单的登录操作

import java.util.Scanner;
public class Demo2 {
    public static void main(String[] args) {
        // 设置用户名和密码
        String username = "admin";
        String password = "123";

        int n = 3;
        for (int i = 0; i < n; i++) {  // 因为只有三次机会,所以设置3次循环
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入用户名:");
            String scUsername = sc.nextLine();  // 获取输入的用户名
            System.out.print("请输入密码:");
            String scPassword = sc.nextLine();  // 获取输入的密码
            if (username.equals(scUsername) && password.equals(scPassword)) {  // 将输入的用户名和密码进行对比
                System.out.println("登陆成功!");  // 如果相同就直接显示登陆成功,退出循环
                break;
            } else {
                if (i == 2){  // 第三次输入的用户名和密码对不上直接输出达到次数上限,结束循环
                    System.out.println("登录次数已经达到上限。");
                } else {
                    System.out.println("用户名或者密码错误,还有"+(2-i)+"次机会!!!");
                }
            }
        }

    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现简单计算器的方法如下: 1. 定义一个Calculator类,并在其添加适当的成员变量和方法。 2. 在Calculator类,定义一个名为calculate的方法,该方法接受三个参数:操作数1、操作符和操作数2,并返回计算结果。 3. 在calculate方法,使用switch语句根据操作符的不同执行相应的计算操作。例如,对于加法操作符,可以使用操作数1 + 操作数2来计算结果。 4. 在主程序,创建一个Calculator对象,并调用其calculate方法进行计算。 以下是一个简单Java代码示例: ```java import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入操作数1:"); double operand1 = scanner.nextDouble(); System.out.print("请输入操作符:"); char operator = scanner.next().charAt(0); System.out.print("请输入操作数2:"); double operand2 = scanner.nextDouble(); double result = calculate(operand1, operator, operand2); System.out.println("计算结果:" + result); scanner.close(); } public static double calculate(double operand1, char operator, double operand2) { double result = 0; switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: System.out.println("无效的操作符!"); break; } return result; } } ``` 这个简单的计算器可以接受用户输入操作数和操作符,并根据操作符进行相应的计算操作,最后输出计算结果。用户可以根据需要输入不同的操作数和操作符进行计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值