2016-05-22 41 views
-2

Ich schreibe ein Programm für die Schule und möchte überprüfen, ob das eingegebene Passwort korrekt ist, aber das Programm läuft weiter, selbst wenn das Passwort falsch ist. Unten ist mein gesamtes Programm!Programm überprüfen, ob das Passwort korrekt ist

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

Vielleicht 'Arrays.equals versuchen (Passwort, passw)' – MadProgrammer

+1

Mögliches Duplikat [Vergleich Arrays in Java] (http://stackoverflow.com/questions/5588460/comparing-arrays- in-java) – MadProgrammer

+0

Ich habe diese Methode versucht, aber kein Glück. Ich habe die Passwort-Methode in einem separaten größeren Programm und wenn das Passwort falsch ist, wird es nicht verlassen. –

Antwort

2

Ein schneller Test zeigt, dass ...

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"); 
} 

druckt Are equal und

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"); 
} 

druckt Are not equal

Dies beweist, in Konzept, dass Arrays.equals arbeitet

Ein kurzer Blick auf den JOptionPane JavaDocs for showOptionDialog Highlights

Returns:
eine ganze Zahl, die den vom Benutzer gewählten Option oder CLOSED_OPTION, wenn der Benutzer den Dialog

Während ein wenig dunkel, diese geschlossen Das bedeutet, dass in Ihrem Fall 0 zurückgegeben wird, wenn der Benutzer wählt Geben Sie oder 1 des Benutzers wählt Abbrechen

So, bezogen auf das, Sie Code funktioniert nur, wenn der Benutzer

Diese Abbrechen wählt ist, wo System.out.println(...) wirklich nützlich ist

Sie eigentlich etwas aussehen soll also aus den aktuellen Werten von Variablen drucken wie mehr ...

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); 
    } 
} 
Verwandte Themen