我正在學習java,至今我已經使用if語句創建了密碼檢查。然而,我插入我的工作字符串檢查到一個while循環,並添加Thread.sleep(3000);對於3秒的延遲,但是一旦我完成了,我的GUI只是在一頁上保持滯後和凍結,就好像按下按鈕一樣。有人可以告訴我如何做一個帶有字符串檢查的代碼的工作示例,並且在一定數量的嘗試延遲以阻止用戶再次嘗試之後? (這裏是我:)java-如何創建一個延遲一定時間的字符串檢查?
//var declaration
boolean match = false;
String1 = "hi";
String2 = (I know this is not code but just to omit some code:) userInput
int time = 3000;
int attempt = 0;
//check
while(!match && attempt < (maximumTries+1)){
if(String1.equals(String2)){
System.out.print("match");
}
else if(attempt < 11){
attempt++;
System.out.println("Failed:" + attempt);
}
else{
attempt++;
System.out.println("Please try again later you have:" + attempt + "failed attempts");
try{
Thread.sleep(time);
}
catch(InterruptedException ex) {
Logger.getLogger(PasswordEntry.class.getName()).log(Level.SEVERE, null, ex);
}
time = time + 1000;//1 second more every time
}
}
使用[計時器](https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html)而不是睡眠。 – BladeMight
我會如何在特定的時間使用計時器?計時器(時間)? –
請看這裏https://stackoverflow.com/questions/2258066/java-run-a-function-after-a-specific-number-of-seconds – BladeMight