java - 如何在不按运行按钮的情况下使程序重新运行
问题描述
我正在尝试制作一个程序来测试密码是否有效。提交密码后,我想让用户选择重新运行程序(通过响应'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');
}
}
解决方案
只要不按下“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');
}
}
推荐阅读
- javascript - 如何在 angularjs 和 PHP 之间添加路由参数
- python - pandas 的函数 to_excel 产生了意外的 TypeError
- jestjs - 如何测试函数内部的函数是否使用正确的参数调用
- c# - 服务器在 AngularJS 中响应状态为 500(内部服务器错误)
- c++ - 这算作递归吗?
- laravel - Laravel Eloquent:来自一个主键的多个外键
- r - 在 R 中创建范围条形图
- .net-core - DynamoDB 如何使用 .net 核心获取分区键的项目数?
- docker - 在 docker 中启用网络管理器
- angular - 三个 JS + Angular 7 导入控制器后出现错误 BufferAttribute