Java 为什么netbeans中的这个登录程序可以';执行得不好?

Java 为什么netbeans中的这个登录程序可以';执行得不好?,java,netbeans,Java,Netbeans,我有一个关于netbeans中java程序的问题要问你们。 我在JavaNetbeans项目中创建了这个登录程序,但这里有一些问题。有人能帮我找出算法中的错误吗? --首先,下面是我的脚本: public static void main(String[] args) { int i; int x; int amountOfData=0; String user=""; String pass=""; String[] username = ne

我有一个关于netbeans中java程序的问题要问你们。 我在JavaNetbeans项目中创建了这个登录程序,但这里有一些问题。有人能帮我找出算法中的错误吗? --首先,下面是我的脚本:

public static void main(String[] args) {

    int i;
    int x;
    int amountOfData=0;
    String user="";
    String pass="";
    String[] username = new String[100];
    String[] password = new String[100];
    boolean found=false;

    Scanner sc = new Scanner(System.in);

    i=0;
    while(i<3){
        System.out.print("Input username -"+(i+1)+":");
        username[i] = sc.nextLine();
        System.out.print("Input password -"+(i+1)+":");
        password[i] = sc.nextLine();
        System.out.println("");
        i++;
        amountOfData++;
    }

    System.out.println("============================");
    System.out.println("Welcome in Login Form");
    System.out.println("============================");
    for(x=1;x<=3;x++){
        System.out.print("Username :");
        user = sc.nextLine();
        System.out.print("Password :");
        pass = sc.nextLine();
        i=0;
        while(i<amountOfData && found==false){
            **if(user==username[i] && pass==password[i])
                found=true;**
            else{
                System.out.print("haha");
                i++;
            }
        }
        if(found==true){
            System.out.println("You Succesfully Login");
            break;
        }
        else{
            System.out.println("Error ! Please Try again !");
        }
    }

} 
publicstaticvoidmain(字符串[]args){
int i;
int x;
int amountOfData=0;
字符串user=“”;
字符串pass=“”;
字符串[]用户名=新字符串[100];
字符串[]密码=新字符串[100];
布尔值=false;
扫描仪sc=新的扫描仪(System.in);
i=0;

而(i在Java中,使用
==
测试引用是否相等(相同的对象),其中as
.equals()
测试值是否相等


因此,当您想要测试两个字符串是否具有相同的值时,您需要使用
.equals()
而不是
=

您的问题与您正在使用==或!=处理不同的字符串对象这一事实有关。为了更正您的程序,您需要对此进行更改
if(user==username[i]&&pass==password[i])
if(user.equals(username[i])&&pass.equals(password[i])
。这与==或!=比较对象不是基于您指定给它们的任意值,而是基于对象本身这一事实有关。

我甚至不明白您在这里试图实现什么。阅读有关
字符串
==
等于
的内容。谢谢,它现在完全起作用了。@FarhanFauz如果很高兴它起作用,请选择评论左侧的复选标记,将答案标记为已接受。