廢話不多說了,關(guān)鍵代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import java.util.*; public class Demo04 { public static void main(String[] args){ //聲明變量 String root= "jim" ; //用戶名 int passwd= 123456 ; //密碼 int time= 0 ; //循環(huán)次數(shù) int sum= 0 ; //總計(jì)次數(shù) Scanner input= new Scanner(System.in); //獲取鍵盤輸入 //for循環(huán)內(nèi) for (time= 2 ;time<= 3 &&time>= 0 ;time--){ //用戶輸入 System.out.print( "請輸入用戶名" ); root=input.next(); System.out.print( "請輸入密碼" ); passwd=input.nextInt(); sum++; //判斷用戶輸入用戶名是否是jim并且密碼=123456; if ( "jim" .equals(root)&&passwd== 123456 ){ System.out.println( "歡迎登陸MyShopping系統(tǒng)" ); break ; } //當(dāng)用戶輸入次數(shù)少于三時(shí) else if (sum< 3 ){ System.out.println( "輸入錯(cuò)誤,您還有" +time+ "次機(jī)會\n" ); continue ; //當(dāng)用戶輸入3次錯(cuò)誤后; } else if (sum== 3 ){ System.out.println( "輸入錯(cuò)誤,您還有" +time+ "次機(jī)會\n" ); System.out.println( "對不起,您3次均輸入錯(cuò)誤" ); } } } } |
以上所述是小編給大家介紹的Java用戶登錄驗(yàn)證代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://11860412.blog.51cto.com/11850412/1841870