字符串分配不起作用?

时间:2014-03-15 17:42:31

标签: java

在我的最后一个else-if语句中(在程序结束时)我打印出两个变量(CodeCheck和cPass)我要检查以下if-else语句。值是正确的,包括它们的长度。但是,它不会进入if语句(cPass ==" Skyfall"),这意味着它将其视为false。如果我在if-else语句正常工作之前分配字符串(即cPass =" Skyfall")(它将其视为真)。

我不明白为什么,以下似乎在if-else语句中不起作用:

cPass = String.format("%s",e.getActionCommand());

同样地:

CodeCheck = String.format("%d%d%d", cCode[0], cCode[1], cCode[2]);

但是,它可以打印这些变量的值(及其长度)。

以下是该计划的所有代码:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class SecurityWindow extends JFrame{


     JButton n1 = new JButton("1");
     JButton n2 = new JButton("2");
     JButton n3 = new JButton("3");
     JButton n4 = new JButton("4");
     JButton n5 = new JButton("5");
     JButton n6 = new JButton("6");
     JButton n7 = new JButton("7");
     JButton n8 = new JButton("8");
     JButton n9 = new JButton("9");

     JTextField username = new JTextField("username");
     JPasswordField password = new JPasswordField("password");

     JButton play;


    public SecurityWindow(){
        super("Security Passage");
        setLayout(new FlowLayout());

        add(username);
        add(password);

        add(n1);
        add(n2);
        add(n3);
        add(n4);
        add(n5);
        add(n6);
        add(n7);
        add(n8);
        add(n9);

        Icon dec = new ImageIcon(getClass().getResource("faceb.png"));
        play = new JButton("Login", dec);
        add(play);

        HandlerClass handler = new HandlerClass();
        username.addActionListener(handler);
        password.addActionListener(handler);
        n1.addActionListener(handler);
        n2.addActionListener(handler);
        n3.addActionListener(handler);
        n4.addActionListener(handler);
        n5.addActionListener(handler);
        n6.addActionListener(handler);
        n7.addActionListener(handler);
        n8.addActionListener(handler);
        n9.addActionListener(handler);
        play.addActionListener(handler);

    }

    int cCode[] = {10,10,10};
    String cUser = "";
    String cPass = "";
    String CodeCheck = "";

    private class HandlerClass implements ActionListener{
        public void actionPerformed(ActionEvent e){

              if(e.getSource() == username){
                 cUser = String.format("%s",e.getActionCommand());
             }else if(e.getSource() == password){
                 cPass = String.format("%s",e.getActionCommand());
             }else if(e.getSource() == n1){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n2){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                     System.out.println(cCode[0]);      
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n3){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n4){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n5){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n6){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n7){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n8){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == n9){
                 if(cCode[0] == 10){
                     cCode[0] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[1] == 10){
                     cCode[1] = Integer.parseInt(e.getActionCommand());
                 }else if(cCode[2] == 10){
                     cCode[2] = Integer.parseInt(e.getActionCommand());
                 }
             }else if(e.getSource() == play){

                 CodeCheck = String.format("%d%d%d", cCode[0], cCode[1], cCode[2]);

                 System.out.println(CodeCheck);
                 System.out.println(cPass);

                 if(cPass == "Skyfall"){
                     if(CodeCheck == "558"){
                         JOptionPane.showMessageDialog(null, cUser + ", you have cracked the security system");
                     }else{
                     System.out.println("cPass wrong");
                     }
                 }else{
                     JOptionPane.showMessageDialog(null, "No Access Granted " + cUser);
                     /*JOptionPane.showMessageDialog(null, CodeCheck);
                     JOptionPane.showMessageDialog(null, cPass);
                     JOptionPane.showMessageDialog(null, cUser);*/

                     System.out.println(CodeCheck);
                     System.out.println(cPass);
                     System.out.println(cUser);
                     System.out.println(cPass.length());
                     System.out.println(CodeCheck.length());
                 }
             }
        }
    }
}

提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用equals()方法进行字符串比较。使用==运算符,可以比较两个String的内存地址。使用equals()方法,您可以比较两个String的值。而不是

if(cPass == "Skyfall")

使用

if(cPass.equals("Skyfall"))