首页 > 解决方案 > 如何在不按运行按钮的情况下使程序重新运行

问题描述

我正在尝试制作一个程序来测试密码是否有效。提交密码后,我想让用户选择重新运行程序(通过响应'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');
   }
}

标签: java

解决方案


只要不按下“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');
    }
}

推荐阅读