亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用循環確定電子郵件地址是否包含 2 個字符

使用循環確定電子郵件地址是否包含 2 個字符

慕村225694 2022-10-12 10:19:37
檢查電子郵件地址是否有效。有效的電子郵件地址應同時包含 at 符號 (@) 或點 (.)。如果用戶省略了其中任何一個,您的程序必須通知用戶提供的電子郵件不正確,并提示用戶重新輸入電子郵件地址。繼續重復此步驟,直到用戶提供有效的電子郵件地址。當用戶輸入有效的電子郵件地址時,繼續執行程序的其余部分。- 我們應該為此使用循環,但我不確定使用哪個或如何設置它。我在考慮使用while循環?我們不能使用我在這里看到人們使用的正則表達式方法,因為我們還沒有學到這一點。This is the code that we have to update(we are now supposed to use a loop to continue asking the user for email address until both characters are used, instead of exiting the program):if(emailAddress.contains("@")){     //prompt user for major & classification code    System.out.print("\nPleast enter two characters (Character #1: Major Code and Character #2: Classification Code): ");    } else{     //exit program if no '@' symbol in email address    System.out.print("\nYou have entered an invalid email address.");    System.out.println("\n\nGoodbye!");    System.exit(0);}
查看完整描述

2 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

我建議使用while循環。當for您知道需要重復操作多少次時,循環是最好的,但while循環適用于重復未知次數。它可以運行 0 到多次。


至于您的while循環條件,您只想檢查電子郵件地址是否無效。因此,請檢查!emailAddress.contains("@")它是否不包含@,!emailAddress.contains(".")如果不包含.。


您可以使用||來查看任一條件是否為真。


// Prompt the user

System.out.println("Please enter an email address");


String emailAddress =  // read in their email adress 


while(!emailAddress.contains("@") || !emailAddress.contains(".")) {

    System.out.println("You have entered an invalid email address, please enter another.");


    emailAddress = // read in their email address again

}


// Email address now contains "@" and "."


查看完整回答
反對 回復 2022-10-12
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

像這樣的東西應該工作:


// While email does not contain the two characters..

// Keep asking the user to enter the correct email.

while(!emailAddress.contains("@") || !emailAddress.contains(".")){ 

    System.out.print("\nYou have entered an invalid email address.");

    // Add line of code here to ask for email address.

    // Update emailAddress of course

}

//prompt user for major & classification code

System.out.print("\nPleast enter two characters (Character #1: Major Code and Character #2: Classification 

// ...


查看完整回答
反對 回復 2022-10-12
  • 2 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號