-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);
}
}
}
}
也許嘗試'滿足Arrays.equals(密碼,PASSW)' – MadProgrammer
可能的複製到[java中比較數組(http://stackoverflow.com/questions/5588460/comparing-arrays- in-java) – MadProgrammer
我試過這種方法,但沒有運氣。我有一個單獨的更大的程序中的密碼方法,如果密碼錯誤,它不會退出。 –