[英]I don't know how to use while loop to make sure that only correct name can jump out of while loop
基本上我的程序要求用戶輸入一個新名稱,程序將檢查新名稱是否符合所有要求。
是的,名稱應該添加到數組中並打印在外部文件中。 如果不是,它應該要求用戶輸入一個新名稱。
我的問題是,我不知道如何使用 while 循環來確保只有名稱可以跳出循環。
我還在主要方法中簽署了我的問題
public static void main(String[] args)throws FileNotFoundException {
readFile();
listUserName();
while() { // my question is at here that I don't how to use while loop to make
//sure that only the name which pass all the check..method can
//jump out of the loop
inputNewName();
checkduplicate();
checklength();
checkcase();
checkstart();
checknumber();
checkspecial();
}
addNewName();
listUserName();
}
public static void readFile()throws FileNotFoundException {
//read file and reseve in array
Scanner input = new Scanner(new File("users.txt"));
int i=0;
while(input.hasNext()) {
String info=input.next();
userName[i]=info;
i++;
}
}
public static void listUserName() {//print name
for(int i=0;i<userName.length;i++) {
System.out.println(userName[i]);
}
}
public static void inputNewName() {// prompt uder for a new name
System.out.println("Create a new user:");
Scanner console=new Scanner(System.in);
newname=console.next();
}
public static void addNewName()throws FileNotFoundException {//add new name in array
System.out.println("User: \""+newname+"\" added successfully!");
System.out.println("List of usernames: ");
String[] tempuser=new String[userName.length+1];
for(int i=0;i<(userName.length+1);i++) {
if(i<userName.length) {
tempuser[i]=userName[i];
}else if(i==userName.length) {
tempuser[userName.length] =newname;
}
System.out.println(tempuser[i]);
}
userName=tempuser;
PrintStream out=new PrintStream(new File("users.txt"));
for(int i=0;i<userName.length;i++) {
out.println(userName[i]);
}
}
public static void checkduplicate() { //check duplicate
for(int i=0;i<userName.length;i++) {
if(newname.equals(userName[i])) {
System.out.println("Invalid Name.Name already in use.");
}else {
valid=true;
}
}
}
public static void checklength() {//check length
if(newname.length()>7) {
System.out.println("Invalid Name"+"\n"+"Name too long.");
}
if(newname.length()<4) {
System.out.println("Invalid Name"+"\n"+"Name too short.");
}
}
public static void checkcase() {//check case
boolean upcase=false;
boolean lowcase=false;
for(int i=0;i<newname.length();i++) {
if((0+newname.charAt(i))>=65&&(0+newname.charAt(i)<=90)) {
upcase=true;
}else if((0+newname.charAt(i))>=97&&(0+newname.charAt(i))<=122) {
lowcase=true;
}
}
if(upcase==false||lowcase==false) {
System.out.println("Usernames must have lower-case and upper-case");
}
}
public static void checkstart() {
if(((0+newname.charAt(0))<65&&(0+newname.charAt(0)>99))||
((0+newname.charAt(0))<97&&(0+newname.charAt(0))>122)){
System.out.println("Invalid name. Name must start with a letter");
}
}
public static void checknumber() {
boolean check=false;
for(int i=0;i<newname.length();i++) {
if((0+newname.charAt(i))>=48&&(0+newname.charAt(i))<=57) {
check=true;
}
}
if(check!=true) {
System.out.println("Username must have at least one number");
}
}
public static void checkspecial() {
boolean check=false;
for(int i=0;i<newname.length();i++) {
if((0+newname.charAt(i))==33||(0+newname.charAt(i))==35||(0+newname.charAt(i))==63) {
check=true;
}
}
if(check!=true) {
System.out.println("Username must have at least one special character.");
}
}
}
優雅的解決方案是將您的方法設為 boolean 類型,而不是 void 類型。 然后,你可以做
boolean light;
do{
light = true;
if(!inputNewName()) light=false;
if(!checkduplicate()) light=false;
if(!checklength()) light = false;
if(!checkcase()) light = false;
if(!checkstart()) light = false;
if(!checknumber()) light = false;
if(!checkspecial()) light = false;
}while(light==false);
請注意,我將每種方法放在不同的 if 語句中。 這迫使代碼到達每個方法。 如果只有一個方法失敗,您真的不需要調用每個方法,您可以將它們組合在一個 if 語句中,或者實際上是在 while 條件下。
while(!inputNewName() || !checkduplicate() || !checklength() || !checkcase() || !checkstart() || !checknumber() || !checkspecial()) {}
此外,如果您將 boolean 變量創建為 class 的成員,然后在需要時在每個 void 方法中將其設置為 false ,您可以獲得相同的結果 - 盡管我必須建議您不要這樣做,即使它的工作效果很差從設計的角度來看代碼。
說了這么多,老實說,我要做的是:
根據它們在概念上的作用來區分這些方法。 這里有輸入字符串、檢查重復項和檢查格式的方法。 我將創建一種方法,該方法包含稱為“checkFormat()”的每個格式檢查。
然后,我會按照我所說的 boolean 類型的每種方法。
我的時間是:
boolean light;
do
{
light = inputNewName();
if(light) light = checkDuplicate(); //this D should be upper case to follow the camel-case convention.
if(light) light = checkFormat();
} while (!light);
更新:我剛剛意識到您的代碼旨在使用我告訴您不要使用的技術。 您可以隨時使用一個名為“valid”的變量。 雖然我真的建議你不要這樣做,但你可以改為 go :
do{
//all your stuff
} while(!valid);
在您的情況下,您可能希望使用do... while
循環(它與while
循環完全相同,除非您保證運行一次。基本上您希望在do {
和} while(condition)
之間運行代碼, 直到達到某個條件(如您的 while 中所定義)。
因此,在您的實現中,您將在輸入的string
無效時運行 while 循環,它看起來類似於以下內容:
public class WhileLoop {
public WhileLoop() {}
public void run() {
String name;
boolean isValid;
do {
isValid = Boolean.FALSE; // We want to set this to FALSE on each run of the loop.
System.out.println("Create a new user:");
Scanner console = new Scanner(System.in);
name = console.next();
isValid = isValid || checklength(name); // run the checkLength method and perform a logical OR on the result from the method
} while (!isValid); // loop while isValid = FALSE (Not TRUE)
System.out.println("Name: " + name);
}
public boolean checklength(String name) { //check length, return TRUE if valid, else FALSE.
boolean isVaild = Boolean.TRUE; // Set the default return value to FALSE
if(name.length() > 7) {
System.out.println("Invalid Name" + System.lineSeparator() + "Name too long.");
isVaild = Boolean.FALSE; // If not valid then set isVaild to FALSE
}
return isVaild;
}
public static void main (String ... args) {
WhileLoop whileLoop = new WhileLoop();
whileLoop.run();
}
}
請注意,該 checkLength 方法返回一個boolean
,其結果使用邏輯 OR 應用於您的標志。
現在,您只需添加各種檢查,如下所示:
isValid = isValid || checklength(name);
isValid = isValid || checkCase(name);
...
isValid = isValid || checkStart(name);
哦,關於良好實踐的一點點:
System.lineSeparator()
而不是\n
。 不要使用邏輯 OR ( ||
),而是使用 boolean function,如下所示:
isValid = checklength(name); isValid = Boolean.logicalOr(isValid, checkCase(name)); ... isValid = Boolean.logicalOr(isValid, checkStart(name));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.