java程序重复一个文本文件而不是注销选项

问题描述:

我正在为我的初学java类的项目工作。该程序是动物园的认证程序。除了一件事我的程序运行良好。在用户名和密码通过验证后,它应该显示一个文件,上面写着“hello zoo keeper ....”,然后让用户注销。我的问题是,它显示正确的文本文件后,它不会要求注销。它重复文本文件2多次。java程序重复一个文本文件而不是注销选项

例如:

你好,动物园管理员!

作为动物园管理员,您可以访问所有动物的信息和 他们的日常监测日志。这使您可以跟踪他们的喂养习惯,栖息地条件和一般福利。

喂,动物园管理员!

作为动物园管理员,您可以访问所有动物的信息和 他们的日常监测日志。这使您可以跟踪他们的喂养习惯,栖息地条件和一般福利。

喂,动物园管理员!

作为动物园管理员,您可以访问所有动物的信息和 他们的日常监测日志。这使您可以跟踪他们的喂养习惯,栖息地条件和一般福利。

BUILD SUCCESSFUL(总时间:15秒)

我要把我下面的代码,并感谢您的帮助,我可以得到的。

 package AuthenticationSystem; 

    //import necessary java tools 
    import java.io.FileInputStream; 
    import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.security.MessageDigest; 
    import java.util.Scanner; 
    import javax.swing.JOptionPane; 

    public class Authenticationsystem { 

    public static void main(String[] args) throws Exception { 

    Scanner scnr = new Scanner(System.in); 

    // fileinput reading txt iles 
    FileInputStream fis = null; 
    InputStreamReader isr = null; 
    BufferedReader br = null; 
    String txtline = ""; 

    //credentials elements 
    final int NUM_ELEMENTS = 6; 
    String[] storedUsername = new String[NUM_ELEMENTS]; 
    String[] role = new String[NUM_ELEMENTS]; 
    String[] hashPassword = new String[NUM_ELEMENTS]; 
    String username = ""; 
    String password = ""; 
    String hash = ""; 

    int i = 0; 
    int j = 0; 
    int user = 0; 
    boolean verified = false; 

    // user/pw array 
    storedUsername[i] = "griffin.keyes"; 
    hashPassword[i] = "108de81c31bf9c622f76876b74e9285f"; 
    ++i; 
    storedUsername[i] = "rosario.dawson"; 
    hashPassword[i] = "3e34baa4ee2ff767af8c120a496742b5"; 
    ++i; 
    storedUsername[i] = "bernie.gorilla"; 
    hashPassword[i] = "a584efafa8f9ea7fe5cf18442f32b07b"; 
    ++i; 
    storedUsername[i] = "donald.monkey"; 
    hashPassword[i] = "17b1b7d8a706696ed220bc414f729ad3"; 
    ++i; 
    storedUsername[i] = "jerome.grizzlybear"; 
    hashPassword[i] = "3adea92111e6307f8f2aae4721e77900"; 
    ++i; 
    storedUsername[i] = "bruce.grizzlybear"; 
    hashPassword[i] = "0d107d09f5bbe40cade3de5c71e9e9b7"; 
    ++i; 

    //dialog box entry 
    JOptionPane.showMessageDialog(null, "All entries are case sensitive.\n" 
      + "Enter \"quit\" at any time to exit.", "Authentication System", JOptionPane.PLAIN_MESSAGE); 

    // enter username/password 
    username = (String) JOptionPane.showInputDialog(null, "Enter username: ", "Authentication System", JOptionPane.PLAIN_MESSAGE); 
    if (username.equals("quit")) { 
     JOptionPane.showMessageDialog(null, "You have chosen to exit. Goodbye.", "Authentication System", JOptionPane.WARNING_MESSAGE); 
     return; 
    } 
    password = (String) JOptionPane.showInputDialog(null, "Enter password: ", "Authentication System", JOptionPane.PLAIN_MESSAGE); 
    if (password.equals("quit")) { 
     JOptionPane.showMessageDialog(null, "You have chosen to exit. Goodbye.", "Authentication System", JOptionPane.WARNING_MESSAGE); 
     return; 
    } 

    //exit after three checks 
    for (i = 0; i < 3; ++i) { 

     // check user credentials 
     for (j = 0; j < NUM_ELEMENTS; ++j) { 
      if (username.equals(storedUsername[j])) { 
       String original = password; // md5digest 
       MessageDigest md = MessageDigest.getInstance("MD5"); 
       md.update(original.getBytes()); 
       byte[] digest = md.digest(); 
       StringBuilder sb = new StringBuilder(); 
       for (byte b : digest) { 
        sb.append(String.format("%02x", b & 0xff)); 
       } 

       verified = true; 
       user = j; 
       hash = sb.toString(); 
      } 
     } 
     //if wrong entry, re-enter 
     if (!hash.equals(hashPassword[user])) { 
      verified = false; 
      if (i < 2) { 
       JOptionPane.showMessageDialog(null, "Invalid user credentials. " + (2 - i) 
         + " attempt(s) remaining.", "Authentication System", JOptionPane.PLAIN_MESSAGE); 
       username = (String) JOptionPane.showInputDialog(null, "Enter username: ", "Authentication System", 
         JOptionPane.PLAIN_MESSAGE); 
       if (username.equals("quit")) { 
        JOptionPane.showMessageDialog(null, "You have chosen to exit. Goodbye.", "Authentication System", 
          JOptionPane.WARNING_MESSAGE); 
        break; 
       } 
       password = (String) JOptionPane.showInputDialog(null, "Enter password: ", "Authentication System", 
         JOptionPane.PLAIN_MESSAGE); 
       if (password.equals("quit")) { 
        JOptionPane.showMessageDialog(null, "You have chosen to exit. Goodbye.", "Authentication System", 
          JOptionPane.WARNING_MESSAGE); 
        break; 
       } 
      } 
      //closes program if tries exceed 3 
      if ((!verified) && (i == 2)) { 
       JOptionPane.showMessageDialog(null, "Could not verify credentials. Goodbye.", "Authentication System", 
         JOptionPane.ERROR_MESSAGE); 
       break; 
      } 
     } // if user credentials work 
     else { 

      //get input from txt files 
      // user if zookeeper 
      if (username.equals(storedUsername[0]) || username.equals(storedUsername[3])) { 
       fis = new FileInputStream("src\\AuthenticationSystem\\zookeeper.txt"); 
       isr = new InputStreamReader(fis); 
       br = new BufferedReader(isr); 
       while ((txtline = br.readLine()) != null) { 
        System.out.println(txtline); 

       } 
      } //user is a veternarian 
      else if (username.equals(storedUsername[2]) || username.equals(storedUsername[4])) { 
       fis = new FileInputStream("src\\AuthenticationSystem\\veterinarian.txt"); 
       isr = new InputStreamReader(fis); 
       br = new BufferedReader(isr); 
       while ((txtline = br.readLine()) != null) { 
        System.out.println(txtline); 
       } 
      } //user is an admin 
      else { 
       fis = new FileInputStream("src\\AuthenticationSystem\\admin.txt"); 
       isr = new InputStreamReader(fis); 
       br = new BufferedReader(isr); 
       while ((txtline = br.readLine()) != null) { 
        System.out.println(txtline); 

       } 
      } 
      System.out.println(); 
      class logout { 

       public void logout(String original) throws Exception { 
        int i = 0; 
        Scanner input = new Scanner(System.in); 
        while (i == 0) { 
         System.out.println("To log out please press 1 then the enter key"); 
         int result = input.nextInt(); 
         if (result == 1) { 
          System.out.println("You have logged out. Have a great day!"); 
          i = 1; 
         } else { 
          System.out.println("You are still logged in."); 
         } 
        } 
       } 
      } 

     } 
    } 
} 

}

第一件事,我会说,把这个代码放到一个for循环,并把所有的用户信息为二维数组。

// user/pw array 


storedUsername[i] = "griffin.keyes"; 
hashPassword[i] = "108de81c31bf9c622f76876b74e9285f"; 
++i; 
storedUsername[i] = "rosario.dawson"; 
hashPassword[i] = "3e34baa4ee2ff767af8c120a496742b5"; 
++i; 
storedUsername[i] = "bernie.gorilla"; 
hashPassword[i] = "a584efafa8f9ea7fe5cf18442f32b07b"; 
++i; 
storedUsername[i] = "donald.monkey"; 
hashPassword[i] = "17b1b7d8a706696ed220bc414f729ad3"; 
++i; 
storedUsername[i] = "jerome.grizzlybear"; 
hashPassword[i] = "3adea92111e6307f8f2aae4721e77900"; 
++i; 
storedUsername[i] = "bruce.grizzlybear"; 
hashPassword[i] = "0d107d09f5bbe40cade3de5c71e9e9b7"; 
++i; 

你有一个for循环那里,所以你必须知道如何做到这一点。

其次,你的逻辑在这里似乎时髦:

//exit after three checks] 
for (i = 0; i < 3; ++i) { 

    // check user credentials 
    for (j = 0; j < NUM_ELEMENTS; ++j) { 
     if (username.equals(storedUsername[j])) { 

等等。

这肯定是你的问题在哪里,但要找出你将不得不放置断点的特定代码行,逐行执行代码 - 我知道这是细致的,但这是游戏名称是开发者。调试你自己的代码是你作为一名软件工程师可以拥有的最重要的技能 - 没有人会为你做这件事。

把这些“安全检查”分解为功能。如果您不想逐行调试,请添加控制台日志记录以显示应用程序正在执行的操作。代码越容易阅读,对于正在做的事情就会有更多的洞察力。

有很多方法可以为这个猫蒙皮,但是你的代码必须整洁,以供其他开发者使用。代码的“质量”通常取决于其他开发人员能够如何快速分辨出发生了什么。如果该测量适用于此代码,则确实相当差。

+0

谢谢你的帮助这么多特别是对于没有给我答案,怎么回事我会学习。我必须找到合适的休息地点,现在它只打印一次,但不会在程序结束时进入注销选项。任何建议从哪里开始寻找? – tim