有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使程序在不按run按钮的情况下重新运行

我正在尝试制作一个程序来测试密码是否有效。提交密码后,我想让用户选择重新运行程序(通过响应“y”)。在我的代码中,除了最后按下y键时,程序停止而不是重新开始

任何帮助都将不胜感激

import java.util.Scanner;
import java.lang.*;
public class PasswordTest1
{
//Program main method
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      
      String again = "y";
      
      
   //Promt user to enter input
      System.out.print("Enter a password: ");
      String password = scan.nextLine();
   
   //If-else to print whether the password is valid
      if (checkPassword(password)) 
      {
         System.out.println("Valid Password");
         System.out.println("Re-run Program (y/n)? ");
         again = scan.next(); 
      } else {
         System.out.println("Invalid Password");
         System.out.println("Re-run Program (y/n)? ");
         again = scan.next(); 
      }
   
   }

//Program new method

   public static boolean checkPassword(String password) {
   String again = "y";
   while((again == "Y") || (again == "y")){
      boolean checkPassword = true;
      {
      
      //Make sure password is at least 7 digits
         if (password.length() < 8) {
            checkPassword = false; 
         }
         else {
         
         //A password has at least 1 digit
            int numberOfDigit = 0;
            for (int i = 0; i < password.length(); i++) {
               if (isDigit(password.charAt(i))) {
                  if(isDigit(password.charAt(i))){
                     numberOfDigit++;
                  }
               }
            }
            if (numberOfDigit < 1) {
               checkPassword = false;
            }
         //Check for lower case
            int numberOfLowerCase = 0;
            for (int i = 0; i < password.length(); i++) {
               if (isLowerCase(password.charAt(i))) {
                  if(isLowerCase(password.charAt(i))){
                     numberOfLowerCase++;
                  }
               }
            }
            
            if (numberOfLowerCase < 1) {
               checkPassword = false;
            }
            //Check for upper case
            int numberOfUpperCase = 0;
            for (int i = 0; i < password.length(); i++) {
               if (isUpperCase(password.charAt(i))) {
                  if(isUpperCase(password.charAt(i))){
                     numberOfUpperCase++;
                  }
               }
            }
               
            if (numberOfUpperCase < 1) {
               checkPassword = false;
            }
               //Check for Special Characters
            int numberOfSpecialCharacters = 0;
            String specialCharactersString = "!@#$%^&*()'+,-./:;<=>?[]_{}|";
            for (int i = 0; i < password.length(); i++) {
               char ch = password.charAt(i);
               if(specialCharactersString.contains(Character.toString(ch))){
                  numberOfSpecialCharacters++;
               }
            }
               
            if (numberOfSpecialCharacters < 1) {
               checkPassword = false;
            }
            
         
         
         
         }
      
      
      }
      return checkPassword;
   }
   
   return true;
   }
   
   
   public static boolean isDigit(char ch) {
      return (ch <= '9' && ch >= '0');
   }

   public static boolean isLowerCase (char ch) {
      return (ch <= 'z' && ch >= 'a');
   }

   public static boolean isUpperCase (char ch) {
      return (ch <= 'Z' && ch >= 'A');
   }
}

共 (1) 个答案

  1. # 1 楼答案

    只要不按n键,我就能让你的代码重复。现在,如果按下除“n”以外的任何键,它将继续

    一个问题是,while循环需要位于主循环的内部。按照设置方式,一旦checkPassword返回,程序就无法重复。询问y/n无法回忆该功能。正如@PM 77-1在评论中提到的,您的字符串比较也有问题。我建议将花括号隔开,这样更容易阅读,因为你有一对没有用的括号

    import java.util.Scanner;
    import java.lang.*;
    public class PasswordTest1
    {
        //Program main method
        public static void main(String[] args)
        {
            String again;
            String n = "n";
            
            while(true)
            {
                //Prompt user to enter input
                Scanner scan = new Scanner(System.in);
                System.out.println("\nEnter a password: ");
                String password = scan.nextLine();
                
                //If-else to print whether the password is valid
                if (checkPassword(password)) 
                {
                    System.out.println("\nValid Password");
                    System.out.println("Re-run Program (y/n)? ");
                    again = scan.next();
                    
                }
                else
                {
                    System.out.println("\nInvalid Password");
                    System.out.println("Re-run Program (y/n)? ");
                    again = scan.next();
                }
                
                if(again.equals(n))
                {
                    break;
                }
            }
        }
    
        //Program new method
        public static boolean checkPassword(String password)
        {
            boolean checkPassword = true;
      
            //Make sure password is at least 7 digits
            if (password.length() < 8)
            {
                checkPassword = false; 
            }
            else
            {
                //A password has at least 1 digit
                int numberOfDigit = 0;
                for (int i = 0; i < password.length(); i++)
                {
                    if (isDigit(password.charAt(i)))
                    {
                        if(isDigit(password.charAt(i)))
                        {
                            numberOfDigit++;
                        }
                    }
                }
                if (numberOfDigit < 1)
                {
                    checkPassword = false;
                }
        
                //Check for lower case
                int numberOfLowerCase = 0;
                for (int i = 0; i < password.length(); i++)
                {
                    if (isLowerCase(password.charAt(i)))
                    {
                        if(isLowerCase(password.charAt(i)))
                        {
                            numberOfLowerCase++;
                        }
                    }
                }
                if (numberOfLowerCase < 1)
                {
                   checkPassword = false;
                }
                
                //Check for upper case
                int numberOfUpperCase = 0;
                for (int i = 0; i < password.length(); i++)
                {
                    if (isUpperCase(password.charAt(i)))
                    {
                        if(isUpperCase(password.charAt(i)))
                        {
                            numberOfUpperCase++;
                        }
                    }
                }
                if (numberOfUpperCase < 1)
                {
                    checkPassword = false;
                }
               
                //Check for Special Characters
                int numberOfSpecialCharacters = 0;
                String specialCharactersString = "!@#$%^&*()'+,-./:;<=>?[]_{}|";
                for (int i = 0; i < password.length(); i++)
                {
                    char ch = password.charAt(i);
                    if(specialCharactersString.contains(Character.toString(ch)))
                    {
                        numberOfSpecialCharacters++;
                    }
                }
                if (numberOfSpecialCharacters < 1)
                {
                    checkPassword = false;
                }
            }
            return checkPassword;   
        }
       
       
        public static boolean isDigit(char ch)
        {
            return (ch <= '9' && ch >= '0');
        }
    
        public static boolean isLowerCase (char ch)
        {
            return (ch <= 'z' && ch >= 'a');
        }
    
       public static boolean isUpperCase (char ch)
        {
            return (ch <= 'Z' && ch >= 'A');
        }
    }