用户登录系统的功能当【用户名】和【密码】不完全正确,则进行3次机会的输入,超过3次,则不能登陆怎么实现啊?下面是我的代码,用数组存储用户名和密码为什么我的用户名和密码相符却不能输出呢?如果有错请高手帮我改一下!万分感谢!
import java.util.Scanner;
public class test {public static class Tset { public static void main(String[] args) {
printmethod1();
Scanner sc=new Scanner(System.in);
try
{
int i;
i=sc.nextInt();
if (i==1){
login();
}
else if(i==2){
}//还未完成的功能
else if(i==3){
}//还未完成的功能
else System.out.println("请重新输入:");
}
catch(Exception e)
{
System.out.println("请输入数字!");
}
}
public static void printmethod1(){
System.out.println(" "+"欢迎使用我行我素购物管理系统");
System.out.println("***********************************");
System.out.println(" "+"1.用户登录" );
System.out.println(" "+"2.更改管理员密码" );
System.out.println(" "+"3.退出" );
System.out.println("***********************************");
System.out.print("请选择,请输入数字:");
}
public static void login(){
String x="",y="";
String [] usernm= new String [20];
usernm[1]="manager";
String [] passwd=new String [20];
passwd [1]="0000";
for (int k=1;k<3;k++){
System.out.print("请输入用户名:"+x);
Scanner username=new Scanner(System.in);
Scanner password=new Scanner(System.in);
x=username.next();
System.out.print("请输入密码:"+y);
y=password.next();
boolean foundit=false;
search:
for(int i=0;i<usernm.length;i++){
if(x==usernm[i]){
for(int j=0;j<passwd.length;j++){
if (y==passwd[j]){
foundit=true;
}
}
}
}
if(foundit){
printmethod2();
}
}
}
public static void printmethod2(){
System.out.println(" "+"欢迎使用我行我素购物管理系统");
System.out.println("***********************************");
System.out.println(" "+"1.客户信息管理" );
System.out.println(" "+"2.购物结算" );
System.out.println(" "+"3.真情回馈" );
System.out.println(" "+"4.注销" );
System.out.println("***********************************");
System.out.print("请选择,请输入数字:");
}
}}
import java.util.Scanner;
public class test {public static class Tset { public static void main(String[] args) {
printmethod1();
Scanner sc=new Scanner(System.in);
try
{
int i;
i=sc.nextInt();
if (i==1){
login();
}
else if(i==2){
}//还未完成的功能
else if(i==3){
}//还未完成的功能
else System.out.println("请重新输入:");
}
catch(Exception e)
{
System.out.println("请输入数字!");
}
}
public static void printmethod1(){
System.out.println(" "+"欢迎使用我行我素购物管理系统");
System.out.println("***********************************");
System.out.println(" "+"1.用户登录" );
System.out.println(" "+"2.更改管理员密码" );
System.out.println(" "+"3.退出" );
System.out.println("***********************************");
System.out.print("请选择,请输入数字:");
}
public static void login(){
String x="",y="";
String [] usernm= new String [20];
usernm[1]="manager";
String [] passwd=new String [20];
passwd [1]="0000";
for (int k=1;k<3;k++){
System.out.print("请输入用户名:"+x);
Scanner username=new Scanner(System.in);
Scanner password=new Scanner(System.in);
x=username.next();
System.out.print("请输入密码:"+y);
y=password.next();
boolean foundit=false;
search:
for(int i=0;i<usernm.length;i++){
if(x==usernm[i]){
for(int j=0;j<passwd.length;j++){
if (y==passwd[j]){
foundit=true;
}
}
}
}
if(foundit){
printmethod2();
}
}
}
public static void printmethod2(){
System.out.println(" "+"欢迎使用我行我素购物管理系统");
System.out.println("***********************************");
System.out.println(" "+"1.客户信息管理" );
System.out.println(" "+"2.购物结算" );
System.out.println(" "+"3.真情回馈" );
System.out.println(" "+"4.注销" );
System.out.println("***********************************");
System.out.print("请选择,请输入数字:");
}
}}
import java.util.Scanner;public class LoginTest {
public static void main(String[] args) {
printmethod1();
Scanner sc = new Scanner(System.in);
int count = 0;
try {
while (true) {
if (count == 4) {
System.out.println("您已经输入超过3次,系统退出!");
return;
}
int i;
i = sc.nextInt();
if (i == 1) {
login();
break;
} else if (i == 2) { }// 还未完成的功能
else if (i == 3) { }// 还未完成的功能
else {
count++;
System.out.println("请重新输入:");
}
}
} catch (Exception e) {
System.out.println("请输入数字!");
} } public static void printmethod1() {
System.out.println(" " + "欢迎使用我行我素购物管理系统");
System.out.println("***********************************");
System.out.println(" " + "1.用户登录");
System.out.println(" " + "2.更改管理员密码");
System.out.println(" " + "3.退出");
System.out.println("***********************************");
System.out.print("请选择,请输入数字:"); } public static void login() {
String x = "", y = "";
String[] usernm = new String[20];
usernm[1] = "manager";
String[] passwd = new String[20];
passwd[1] = "0000";
for (int k = 1; k < 3; k++) {
System.out.print("请输入用户名:" + x);
Scanner username = new Scanner(System.in);
Scanner password = new Scanner(System.in);
x = username.next();
System.out.print("请输入密码:" + y);
y = password.next(); boolean foundit = false;
search: for (int i = 0; i < usernm.length; i++) {
if (x == usernm[i]) {
for (int j = 0; j < passwd.length; j++) {
if (y == passwd[j]) {
foundit = true;
}
}
}
}
if (foundit) {
printmethod2();
}
}
} public static void printmethod2() {
System.out.println(" " + "欢迎使用我行我素购物管理系统");
System.out.println("***********************************");
System.out.println(" " + "1.客户信息管理");
System.out.println(" " + "2.购物结算");
System.out.println(" " + "3.真情回馈");
System.out.println(" " + "4.注销");
System.out.println("***********************************");
System.out.print("请选择,请输入数字:"); }}
if (x == usernm[i]) {
for (int j = 0; j < passwd.length; j++) {
if (y == passwd[j]) {
foundit = true;
}
}
}
}
不能用 == 号,好好看看书for (int i = 0; i < usernm.length; i++) {
if (x.equals(usernm[i])) {
for (int j = 0; j < passwd.length; j++) {
if (y.equals(passwd[j])) {
foundit = true;
}
}
}
}
search:
for(int i=0;i<usernm.length;i++){
if(x.equals(usernm[i])){
for(int j=0;j<passwd.length;j++){
if (y.equals(passwd[j])){
foundit=true;
break search;
}
}
}
}