简体   繁体   English

如何解决while循环错误登录注销系统(无数据库)的问题(还有关于会话时间的代码)?

[英]How to fix while loop error login logout system (no database) (also is there a code about session time)?

I create a basic login and logout system (no database) with the only username given for my school homework, but I have problems with while loops and also session time. 我使用为学校家庭作业指定的唯一用户名创建了一个基本的登录和注销系统(无数据库),但是while循环以及会话时间都存在问题。

I tried to copy and paste a group of codes to different parts of this main code so that I can get my expected outcome but it turns out to be a bit faulty. 我试图将一组代码复制并粘贴到此主代码的不同部分,以便可以得到预期的结果,但事实证明这是有问题的。 I tried to search on the internet about session time but I got nothing. 我试图在互联网上搜索会话时间,但一无所获。

  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!");
          }   
      }
}
  • This must be the expected output: 这必须是预期的输出:

if the name is correct: 如果名称正确:

Enter your name:
nmae
--------------------

Date and Time of login:
2019/02/03 16:38:46

--------------------

Enter your name to log out:
nmae

--------------------

Date and Time of logout:
2019/02/03 16:38:46
Session Time:
(This must show how many minutes and seconds a client uses the program)

--------------------
Do you want to register again? 0 for yes and 1 for no
0
Enter your name:

incorrect and it is corrected later on: 不正确,稍后会更正:

Enter your name:
name
--------------------

Date and Time of login:
2019/02/03 16:38:46

--------------------

Enter your name to log out:
nmae

Enter your name to log out:
name

but it turns out that on second loop, third loop and so on, the program asks me to "enter your name to log out" and "Do you want to register again? 0 for yes and 1 for no" instead. 但是事实证明,在第二个循环,第三个循环等上,程序要求我“输入您的名称以注销”和“是否要重新注册?0表示是,1表示否”。 The "enter your name" part prints instead of asking my name. 打印“输入您的姓名”部分,而不是询问我的名字。

and then when I enter 0 to exit, this error showed up C:\\Users\\DELL\\AppData\\Local\\NetBeans\\Cache\\10.0\\executor-snippets\\run.xml:111: The following error occurred while executing this line: C:\\Users\\DELL\\AppData\\Local\\NetBeans\\Cache\\10.0\\executor-snippets\\run.xml:94: Java returned: 1 然后当我输入0退出时,出现此错误C:\\Users\\DELL\\AppData\\Local\\NetBeans\\Cache\\10.0\\executor-snippets\\run.xml:111: The following error occurred while executing this line: C:\\Users\\DELL\\AppData\\Local\\NetBeans\\Cache\\10.0\\executor-snippets\\run.xml:94: Java returned: 1

You are returning an error level 1, which is the way a program terminates returning an error to the OS. 您正在返回错误级别1,这是程序终止将错误返回到OS的方式。 I can not test is as I do not use Windows, but you could try replacing System.exit(1) with System.exit(0) . 我无法测试是因为我不使用Windows,但是您可以尝试将System.exit(1)替换为System.exit(0)

To get difference beetwen login time and logout time you can use Duration class from java8: 要获取不同的beetwen登录时间和注销时间,可以使用java8中的Duration类:

loginTime = LocalDateTime.now();
...
logoutTime = LocalDateTime.now();

Duration.between(loginTime, logoutTime).getSeconds();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM