package Hamber_Bao;
import java.util.Scanner;
public class Login {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String manager = "admin";
String password = "admin";
boolean xn = false;
int i = 0;
do {
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
String pass = input.next();
xn = (!(name.equals(manager)) || !(pass.equals(password)));
if (xn) {
System.out.println("您输入错误!请重新输入!");
i++;
if (i == 3) {
break;
}
}
} while (xn);
if(xn)
System.out.println("您无权限进入该系统!");
else
System.out.println("欢迎光临!");
}
}
首先利用do-while循环接受用户的输入
声明整型变量i计数
声明布尔类型变量,标记用户名和密码是否错误
若错误,提示,i自加,并且判断i是否等于3
若等于3,跳出
好好看看哦··
祝你学业有成!
By 【Hamber_Bao】
/**
* 管理员登陆系统
*/
public class AdminLogin {
public static void main(String [] args){
Scanner as = new Scanner(System.in);
//用户名输入三次登陆系统自动结束
for(int i=3;i>0;i--){
System.out.print("请输入账号:");
String idName=as.next();
System.out.print("请输入密码:");
String password=as.next();
if(idName.equals("你的用户名账号") && password.equals("你的密码")){
//登陆成功,进入主菜单
}else{
System.out.println("您输入的账号或密码不正确,请重新输入,您还有"+(i-1)+"次机会");
}
}
System.out.println("连续三次输入错误,系统被迫退出!");
}
}
新建类Register.java,此类实现密码输入错误三次则系统在三秒后退出,如要实现用户名也错误三次退出,类似可得出,不再赘述。具体代码如下:
********************************************************************
import java.util.Scanner;
/**
* @author Godwin
* @version 2010-05-17
*/
class Register {
private static Scanner scan = new Scanner(System.in);
static String name = "Godwin";
static String password = "godwin";
public static void main(String[] args) {
System.out.println();
System.out.println("****************************");
System.out.println("* Please input your name: *");
System.out.println("****************************");
if (scan.nextLine().trim().equalsIgnoreCase(name)) {
for (int j = 0; j < 3; j++) {
System.out.println("Please input your password: ");
if (scan.nextLine().trim().equals(password)) {
System.out.println("Login success!");
return;
}
}
System.out
.println("You have typed wrong password 3 times, system will be shutdown in 3 seconds!");
try {
for (int j = 3; j > 0; j--) {
System.out.println("Remaining seconds: " + j + ".");
Thread.sleep(1000);
}
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
********************************************************************
密码错误三次运行结果如下:
********************************************************************
****************************
* Please input your name: *
****************************
Godwin
Please input your password:
god
Please input your password:
win
Please input your password:
tyut
You have typed wrong password 3 times, system will be shutdown in 3 seconds!
Remaining seconds: 3.
Remaining seconds: 2.
Remaining seconds: 1.
********************************************************************
正确运行结果:
********************************************************************
****************************
* Please input your name: *
****************************
Godwin
Please input your password:
godwin
Login success!
import java.awt.*;
import hsa.Console;
public class Test
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
String un;
String pw;
String fun = "XXXX";
String fpw = "YYYY";
//最多运行3次
for (int i = 0 ; i < 3 ; i++)
{
c.print ("UserName:");
un = c.readString ();
c.print ("Password:");
pw = c.readString ();
//当用户名和密码都对的话,退出运行
if (un.equals (fun) || pw.equals (fpw))
{
i = 3;
}
}
} // main method
} // Test class