PAT 乙級 1067 試密碼
題目要求
輸入格式:
輸出格式:
輸入樣例 1:
輸出樣例 1:
輸入樣例 2:
輸出樣例 2:
解題代碼
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++; } } }
解題思路:
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)); } }
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)); } }