2016-05-22 25 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); 
    } 
}