日期:2014-05-20 浏览次数:20683 次
import java.util.Scanner; public class UI { protected char[] message; public void printMessage(String message){ System.out.println(this.message); } public String readMessage(){ Scanner sc=new Scanner(System.in); String s = sc.nextLine(); return s; } } class UserLoginUI extends UI{ String user=new String("user"); String pass=new String("pass"); UserLoginUI st; void login(){ System.out.println("请输入用户名和密码:"); if(user.equals(st.readMessage())){ System.out.println("登陆成功"); }else{ System.out.println("登陆失败"); } } } class AdminLoginUI extends UI{ String Admin=new String("user"); String pass=new String("pass"); AdminLoginUI st; void login(){ System.out.println("请输入用户名和密码:"); if(Admin.equals(st.readMessage())){ System.out.println("登陆成功"); }else{ System.out.println("登陆失败"); } } } class TestMain { public static void main(String[] args){ UserLoginUI user1 = new UserLoginUI(); user1.login(); AdminLoginUI admin=new AdminLoginUI(); admin.login(); } }
------解决方案--------------------
下面是帮你改的程序,一般用户是user user登陆,管理员是admin admin登陆
UI类没有改,改了UserLoginUI和AdminLoginUI 还有测试类
对应package你改成自己的包就可以了
public class UserLoginUI extends UI{ String user=new String("user"); String pass=new String("pass"); // UserLoginUI st; public void login(){ System.out.println("请输入用户名"); String inputUser=this.readMessage(); System.out.println("请输入密码"); String password=this.readMessage(); if(user.equals(inputUser)&&pass.equals(password)){ System.out.println("登陆成功"); }else{ System.out.println("登陆失败"); } } }