問題是檢查有效的用戶名:
條件是:
1.用戶名由8到30個字符組成。如果用戶名包含少于 8 個或多于 30 個字符,則它是無效的用戶名。
2.用戶名只能包含字母數字字符和下劃線(_)。字母數字字符描述由小寫字符 [az]、大寫字符 [AZ] 和數字 [0-9] 組成的字符集。
3.用戶名的第一個字符必須是字母字符,即小寫字符[az]或大寫字符[AZ]。
樣本輸入:
8
朱莉婭
薩曼莎
薩曼莎_21
1薩曼莎
薩曼莎?10_2A
朱莉婭Z007
朱莉婭@007
_Julia007
樣本輸出:
無效的
有效的
有效的
無效的
無效的
有效的
無效的
無效的
在某些情況下,我得到錯誤的輸出。
對于輸入: JuliaZ007 ,我應該得到有效但無效。剩下的都是正確的。
我能知道代碼有什么問題嗎?
https://www.hackerrank.com/challenges/valid-username-checker/problem?isFullScreen=false
import java.io.*;
import java.util.*;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int flag=1;
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
String userName = scan.nextLine();
char a[] = userName.toCharArray();
String specialCharactersString = "!@#$%&*()' ,-./:;<=>?[]^`{|}";
for (int i=0;i<userName.length();i )
{
char ch = userName.charAt(i);
if(specialCharactersString.contains(Character.toString(ch)) && Character.isLetterOrDigit(ch)==false)
{
flag=0;
}
}
if (userName.length()>=8 && userName.length()<=30 && Character.isLetter(a[0])==true && flag==1) {
System.out.println("Valid");
} else {
System.out.println("InValid");
}
}
}
}
uj5u.com熱心網友回復:
用戶名由 8 到 30 個字符組成(包括 8 到 30 個字符)。如果用戶名包含少于 8 個或多于 30 個字符,則它是無效的用戶名。
boolean valid = true;
if (userName.length() < 8 || userName.length() > 30) {
valid = false;
}
用戶名只能包含字母數字字符和下劃線
if (valid) {
for (int i = 0; i < userName.length(); i ) {
boolean temp = c
Character c = userName.charAt(i);
valid = Character.isLetterOrDigit(c);
// before invalidating, check to see if it is an underscore
if (!valid) {
valid = c == '_';
if (!valid)
break;
}
}
}
用戶名的第一個字符必須是字母字符
if (valid) {
valid = Character.isLetter(userName.charAt(0));
}
執行所有驗證步驟后,只需return valid;. 附帶說明一下,您可以在回圈時檢查第一個字符的規則以檢查剩余字符的有效性。我只是為了清楚起見做了單獨的檢查,并將所有三個驗證要求分開。
最后,我想指出這很可能不是非正則運算式的最佳解決方案。但是,我在這些步驟中對其進行了分解,因為我強烈認為作為初學者開發人員,您應該以這種方式解決問題:首先將問題分解為單獨的較小問題。然后,獨立提出每個解決方案,最后將較小的解決方案集成到最終產品中。
uj5u.com熱心網友回復:
使用正則運算式
在您的情況下,正則運算式是最合適的:
import java.util.Scanner;
import java.util.regex.Pattern;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
final String regex = "^[a-zA-Z][\\w_]{7,29}$";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
String userName = scan.nextLine();
if (pattern.matcher(userName).matches())
System.out.println("Valid");
else
System.out.println("InValid");
}
}
}
沒有正則運算式
import java.util.Scanner;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
String userName = scan.nextLine();
if (isValid(userName))
System.out.println("Valid");
else
System.out.println("InValid");
}
}
public static boolean isValid(String userName) {
if (!Character.isAlphabetic(userName.charAt(0))
|| userName.length() < 8
|| userName.length() > 30)
return false;
for (char c : userName.toCharArray()) {
if (!Character.isLetterOrDigit(c) && c != '_')
return false;
}
return true;
}
}
解釋導致程式出現問題的原因
你的問題是由flag.
之前JuliaZ007,您有名稱Samantha?10_2A,但此名稱設定flag為0。當你在 中時JuliaZ007,flag總是0你得到一個InValid。
要更正此問題,您可以在每個新名稱上重置flag為1。為此,您可以簡單地移動一下int flag = 1。
例子 :
import java.io.*;
import java.util.*;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
int flag = 1;
String userName = scan.nextLine();
char a[] = userName.toCharArray();
String specialCharactersString = "!@#$%&*()' ,-./:;<=>?[]^`{|}";
for (int i = 0; i < userName.length(); i ) {
char ch = userName.charAt(i);
if (specialCharactersString.contains(Character.toString(ch))
&& Character.isLetterOrDigit(ch) == false) {
flag = 0;
}
}
if (userName.length() >= 8 && userName.length() <= 30 && Character.isLetter(a[0]) == true && flag == 1) {
System.out.println("Valid");
} else {
System.out.println("InValid");
}
}
}
}
uj5u.com熱心網友回復:
請注意,類中的方法java.lang.Character,例如isLetterOrDigit,將檢查任何語言的字符(或數字)。
由于您的問題規定數字必須是通常稱為阿拉伯數字的數字,而字母必須是英文字母表中的數字,因此以下代碼使用用戶輸入的每個字符的unicode 代碼點userName。
import java.util.Scanner;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
String result;
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
result = "invalid";
String userName = scan.nextLine();
int len = userName.length();
if (len >= 8 && len <= 30) {
char[] letters = userName.toCharArray();
if ((letters[0] >= 65 && letters[0] <= 90) || (letters[0] >= 97 && letters[0] <= 122)) {
int i = 1;
for (; i < len; i ) {
if ((letters[i] >= 48 && letters[i] <= 57) ||
(letters[i] >= 65 && letters[i] <= 90) ||
(letters[i] >= 97 && letters[0] <= 122) ||
(letters[i] == 95)) {
continue;
}
break;
}
if (i == len) {
result = "valid";
}
}
}
System.out.println(result);
}
}
}
編輯
只是為了它,這里是一個使用流 API的解決方案。
import java.util.Scanner;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
String result;
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
result = "invalid";
String userName = scan.nextLine();
int len = userName.length();
if (len >= 8 && len <= 30) {
char letter = userName.charAt(0);
if ((letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122)) {
if (userName.chars()
.skip(1L)
.allMatch(c -> (c >= 48 && c <= 57) || // c is a digit
(c >= 65 && c <= 90) || // c is uppercase letter
(c >= 97 && c <= 122) || // c is lowercase letter
(c == 95))) { // c is underscore
result = "valid";
}
}
}
System.out.println(result);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407627.html
標籤:
上一篇:使用字串參考類中的物件?
下一篇:如何垂直求和字串的數字?
