PTA 乙級 1067試密碼 Java代碼

PAT 乙級 1067 試密碼

當你試圖登錄某個系統卻忘了密碼時,系統一般只會允許你嘗試有限多次,當超出允許次數時,賬號就會被鎖死。本題就請你實現這個小功能。

題目要求

輸入格式:

輸入在第一行給出一個密碼(長度不超過 20 的、不包含空格、Tab、回車的非空字符串)和一個正整數 N(≤ 10),分別是正確的密碼和系統允許嘗試的次數。隨後每行給出一個以回車結束的非空字符串,是用戶嘗試輸入的密碼。輸入保證至少有一次嘗試。當讀到一行只有單個 # 字符時,輸入結束,並且這一行不是用戶的輸入。

輸出格式:

對用戶的每個輸入,如果是正確的密碼且嘗試次數不超過 N,則在一行中輸出 Welcome in,並結束程序;如果是錯誤的,則在一行中按格式輸出 Wrong password: 用戶輸入的錯誤密碼;當錯誤嘗試達到 N 次時,再輸出一行 Account locked,並結束程序。

輸入樣例 1:

Correct%pw 3
correct%pw
Correct@PW
whatisthepassword!
Correct%pw
#

輸出樣例 1:

Wrong password: correct%pw
Wrong password: Correct@PW
Wrong password: whatisthepassword!
Account locked

輸入樣例 2:

cool@gplt 3
coolman@gplt
coollady@gplt
cool@gplt
try again
#

輸出樣例 2:

Wrong password: coolman@gplt
Wrong password: coollady@gplt
Welcome in

解題代碼

Java代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
public class Main03 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] input = bf.readLine().split(" ");
        String pwd = input[0];
        int n = Integer.parseInt(input[1]);
        int count = 0;
        String str;
        do {
            str = bf.readLine();
            if (count == n) {
                System.out.println("Account locked");
                break;
            } else {
                if ("#".equals(str)) {
                    break;
                }
                if (!pwd.equals(str)) {
                    System.out.println("Wrong password: " + str);
                } else {
                    System.out.println("Welcome in");
                    break;
                }
                count++;
            }
        } while (!"#".equals(str));
    }
}
 
整理之後的代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] input = bf.readLine().split(" ");
        String pwd = input[0];
        int n = Integer.parseInt(input[1]);
        int count = 0;
        String str;
        while (true) {
            str = bf.readLine();
            if (count == n) {
                System.out.println("Account locked");
                break;
            }
            if ("#".equals(str)) {
                break;
            }
            if (pwd.equals(str)) {
                System.out.println("Welcome in");
                break;
            } else {
                System.out.println("Wrong password: " + str);
            }
            count++;
        }
    }
}
 

解題思路:

在解題過程中需要考慮兩種特殊情況
第一種是直接#結束,不輸入密碼的情況,輸出就結束程序;
第二種是輸入次數用完之後,使用#輸入,會輸出"Account locked",而不是結束程序。
 
第一種輸入:
123456 3
#
第一種輸出:
 
第二種輸入:
123456 3
111111
222222
333333
#
第二種輸出:
Wrong password: 111111
Wrong password: 222222
Wrong password: 333333
Account locked
 
一開始出錯的原因是測試用例2和測試用例4不能通過
測試用例4應該是第二種情況
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
public class Main03 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] input = bf.readLine().split(" ");
        String pwd = input[0];
        int n = Integer.parseInt(input[1]);
        int count = 0;
        String str;
        do {
            str = bf.readLine();
            if ("#".equals(str)) {
                break;
            }
            if (count == n) {
                System.out.println("Account locked");
                break;
            } else {
                
                if (!pwd.equals(str)) {
                    System.out.println("Wrong password: " + str);
                } else {
                    System.out.println("Welcome in");
                    break;
                }
                count++;
            }
        } while (!"#".equals(str));
    }
}

 

這個程序會出現測試用例4不能通過
在IDEAJ中的運行情況:
 
 
 
以下代碼會出現測試用例2不能通過的情況,就是如果在沒有超過錯誤次數時,使用#結束輸入的情況,同時也給出了在IDEAJ中的運行情況
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
public class Main03 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] input = bf.readLine().split(" ");
        String pwd = input[0];
        int n = Integer.parseInt(input[1]);
        int count = 0;
        String str;
        do {
            str = bf.readLine();
            /*if ("#".equals(str)) {
                break;
            }*/
            if (count == n) {
                System.out.println("Account locked");
                break;
            } else {
 
 
                if (!pwd.equals(str)) {
                    System.out.println("Wrong password: " + str);
                } else {
                    System.out.println("Welcome in");
                    break;
                }
                count++;
            }
        } while (!"#".equals(str));
    }
}

 

在IDEAJ中的運行情況:
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章