2016-05-22 49 views
-2

我正在为学校编写程序,我希望它能检查输入的密码是否正确,但即使密码不正确,程序也会继续运行。以下是我的整个计划!让程序检查密码是否正确

public class ma_RocketProgram 
{ 

    static Console c; 


//***********************************Main Program****************************** 

    public static void main (String[] args) 
    { 

    c = new Console (35, 85); 

    c.setColor (Color.black); 
    c.fillRect (0, 0, 700, 550); 

    Image picture1 = loadImage ("USA.png");  
    c.drawImage (picture1, 200, 100, null); 

    IntroMessage(); 

    c.getChar(); 

    Password(); 

    c.setColor (Color.blue); 

    c.drawRect (175, 475, 300, 10); 

    for (int a = 176; a <= 465; a++) 
    { 

     c.setColor (Color.green); 

     c.fillRect(a, 476, 10, 9); 

     try 
     { 

     Thread.sleep(25); 

     } 

     catch (InterruptedException ex) 
     { 

     Thread.currentThread().interrupt(); 

     } 

    } 

    } 

//********************************Image Loader Method************************** 

    public static Image loadImage (String name) 
    { 
    Image img = null; 

    try 
    { 

     img = ImageIO.read (new File (name)); 

    } 

    catch (IOException e) 
    { 

    } 

    return img; 

    } 

//******************************Intro Message************************************* 

    public static void IntroMessage() 

    { 

    Font f = new Font ("Courier New", Font.PLAIN, 30); 

    c.setColor (Color.green); 
    c.setFont (f); 

    c.drawString ("W", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" e", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" l", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" c", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" o", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("  m", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("  e", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("  t", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("   o", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("   t", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("   h", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("    e", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("    R", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("    o", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("     c", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("     k", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("     e", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("     t", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("      L", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("      a", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("      u", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("       n", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("       c", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("       h", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("       e", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("        r", 60, 25); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("P", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" r", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" o", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" g", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString (" r", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("  a", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.drawString ("  m", 250, 60); 

    try 
    { 

     Thread.sleep(50); 

    } 

    catch (InterruptedException ex) 
    { 

     Thread.currentThread().interrupt(); 

    } 

    c.setColor(Color.white); 

    c.drawRect(215, 360, 215, 55); 

    Font f2 = new Font ("Courier New", Font.PLAIN, 15); 

    c.setColor (Color.green); 
    c.setFont (f2); 

    c.drawString ("Press any key to", 245, 375); 
    c.drawString ("display password field", 225, 400); 

    } 

//************************************Password************************* 

    public static void Password() 
    { 

    JPanel panel = new JPanel(); 

    JLabel label = new JLabel ("Enter the password"); 

    JPasswordField pass = new JPasswordField(10); 

    panel.add (label); 
    panel.add (pass); 

    String[] options = new String[]{"Enter", "Cancel"}; 

    int option = JOptionPane.showOptionDialog (null, panel, "Rocket Launcher", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); 

    if (option == 0) 
    { 

     char[] password = pass.getPassword(); 

     char[] passw = new char[]{'1'}; 

     boolean b = Arrays.equals(password, passw); 

     if (b = false) 
     { 

     System.exit(0); 

     } 

    } 

    } 


} 
+0

也许尝试'满足Arrays.equals(密码,PASSW)' – MadProgrammer

+1

可能的复制到[java中比较数组(http://stackoverflow.com/questions/5588460/comparing-arrays- in-java) – MadProgrammer

+0

我试过这种方法,但没有运气。我有一个单独的更大的程序中的密码方法,如果密码错误,它不会退出。 –

回答

2

快速测试显示,...

char[] password = new char[]{'u', 'n', 'k', 'w', 'o', 'n'}; 
char[] passw = new char[]{'u', 'n', 'k', 'w', 'o', 'n'}; 

if (Arrays.equals(password, passw)) { 
    System.out.println("Are equal"); 
} else { 
    System.out.println("Are not equal"); 
} 

打印Are equal

char[] password = new char[]{'u', 'n', 'k', 'w', 'o', 'n'}; 
char[] passw = new char[]{'U', 'n', 'k', 'w', 'o', 'n'}; 

if (Arrays.equals(password, passw)) { 
    System.out.println("Are equal"); 
} else { 
    System.out.println("Are not equal"); 
} 

打印Are not equal

这证明,在概念,即Arrays.equals工作

快速浏览在JOptionPane JavaDocs for showOptionDialog亮点

返回:
一个整数,指示由用户,或CLOSED_OPTION选择的选项,如果用户关闭该对话框

虽然有点模糊,这意味着,在您的情况下,如果用户选择输入1,则用户选择将返回0取消

因此,基于这一点,你的代码将仅当用户选择取消

这是System.out.println(...)是真正有用的,打印出的变量

实际值

所以你其实应该看起来工作更像是...

JPanel panel = new JPanel(); 
JLabel label = new JLabel("Enter the password"); 
JPasswordField pass = new JPasswordField(10); 

panel.add(label); 
panel.add(pass); 

String[] options = new String[]{"Enter", "Cancel"}; 

int option = JOptionPane.showOptionDialog(null, panel, "Rocket Launcher", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); 

if (option == 0) { 
    char[] password = pass.getPassword(); 
    char[] passw = new char[]{'u', 'n', 'k', 'w', 'o', 'n'}; 

    if (Arrays.equals(password, passw)) { 
     System.out.print("Right"); 
    } else { 
     System.exit(0); 
    } 
}