喵喵時(shí)光機(jī)
2022-06-04 10:43:12
我使用為我的學(xué)校作業(yè)提供的唯一用戶名創(chuàng)建了一個(gè)基本的登錄和注銷(xiāo)系統(tǒng)(無(wú)數(shù)據(jù)庫(kù)),但我在 while 循環(huán)和會(huì)話時(shí)間方面遇到了問(wèn)題。我嘗試將一組代碼復(fù)制并粘貼到此主代碼的不同部分,以便獲得預(yù)期的結(jié)果,但結(jié)果有點(diǎn)錯(cuò)誤。我試圖在互聯(lián)網(wǎng)上搜索有關(guān)會(huì)話時(shí)間的信息,但一無(wú)所獲。 while (login = true){ try { System.out.println("Enter your name:"); String name = cue.nextLine(); System.out.println("--------------------"); System.out.println(""); System.out.println("Date and Time of Login:"); System.out.println(dtf.format(now)); System.out.println(""); System.out.println("--------------------"); System.out.println(); System.out.println("Enter your name to log out:"); String logout = cue.nextLine(); System.out.println(""); if (logout.equals(name)){ System.out.println("--------------------"); System.out.println(""); System.out.println("Date and Time of Logout:"); System.out.println(dtf.format(now)); System.out.println("Session Time:"); /*can you also please tell me what code to tell the gap between the login time and log out time?*/ System.out.println(""); System.out.println("--------------------"); login = false; } else { login = true; } } catch (Exception e){ cue.nextLine(); } finally{ System.out.println("Do you want to register again? 0 for yes and 1 for no"); int no = cue.nextInt(); if (no==0) { login = true; } else if (no==1) { System.exit(1); } else { System.out.println("1 or 0 only!"); } }}這必須是預(yù)期的輸出:如果名稱(chēng)正確:Enter your name:nmae--------------------Date and Time of login:2019/02/03 16:38:46--------------------Enter your name to log out:nmae--------------------
2 回答

慕桂英4014372
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
要獲得不同的 beetwen 登錄時(shí)間和注銷(xiāo)時(shí)間,您可以使用Durationjava8 中的類(lèi):
loginTime = LocalDateTime.now();
...
logoutTime = LocalDateTime.now();
Duration.between(loginTime, logoutTime).getSeconds();

人到中年有點(diǎn)甜
TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
您正在返回錯(cuò)誤級(jí)別 1,這是程序終止向操作系統(tǒng)返回錯(cuò)誤的方式。我無(wú)法測(cè)試是因?yàn)槲也皇褂?Windows,但您可以嘗試System.exit(1)
用System.exit(0)
.
添加回答
舉報(bào)
0/150
提交
取消